Issue in obtaining json data from the URL in php - php

Hi all am Working on web service for mobile applications in my case the data from the mobile will be received in the json format the problem is i can't decode the json data only i can get the post values of it
json data send from mobile
test:{"username":"abc1234","emailid":"abc#asd.com","password":"abc123","transactionpassword":"abc1234","phone":"1234567890","secretquestion":"What is the name of my pet?","secretanswer":"Tom"}
the following array obtained by
print_r($_POST);
Array
(
[------WebKitFormBoundaryXb9aAj9gBd1dA41z
Content-Disposition:_form-data;_name] => "test"
{"username":"abc1234","emailid":"abc#asd.com","password":"abc123","transactionpassword":"abc1234","phone":"1234567890","secretquestion":"What is the name of my pet?","secretanswer":"Tom"}
------WebKitFormBoundaryXb9aAj9gBd1dA41z--
)
while i tried to decode it no results were shown
print_r(json_decode($_POST));
empty
Help me to decode the json data in php

It looks like you should fetch actual POST param first.
json_decode($_POST['test'])

Related

Converting POST data from json-text to PHP objects?

I am trying to setup an automatic email handling. I am using external service https://www.email2json.net to convert emails to JSON/text format that is called on a webhook URL in my website.
How can I grab the raw post data and convert it to an object in PHP ?
Hence converting the email jebrish to something meaningful ?
Thanks.
You are getting a JSON object sent via POST, so you should first retrieve the input object with:
$input = file_get_contents("php://input");
Then you can easily handle this JSON object by using the json_decode native function to convert it into an associative array:
$decoded_input = json_decode($input, true);
Hope this helped.

How to read GET and POST request in PHP (post is in json)

I have a javascript (not from me) to sync information between client (javascript) and server (php) and javascript send to server GET and POST information, I can read easy GET information so I try to read $_POST information and cannot retrieve POST information.
I try print_r($_POST) and just returns Array() with nothing.
I try to look it with Firebug and Console into Firefox and I see that
How can I retrieve and treat json string into >Transmission de la requete< into my PHP code? I can easily retrieve GET parameters such as token, src and etc so I can't retrieve POST transmission.
Thank you for your helping !
I don't really get you. But since it's something like JSON request sent to the page. You can use the file_get_contents('php://input') to grab any JSON request sent. Then you can decode it into an object or an array.
Example
$request = file_get_contents('php://input');
To obtain an object
$input = json_decode($request);
So Input fields will be
$input->name; $input->password;
To Obtain an array
$input = json_decode($request, true);
So Input fields will be
$input[name]; $input[password];

JSON array as value getting text

I have created API for creating a customer in PHP, the client side is in Android would use a POST request to access this API. For testing I have used chrome postman where I had used post data as :
{"name":"test","marks":["12","13","16"],"age":19}
and it is working fine.
I am getting marks as array itself ["12","13","16"].
But from android client I am getting as text ["12","13","14"].
What may be the possible reason?
POST it as :
{"name":"test","marks":[12,13,16],"age":19}
You are posting it as String inside array ["12","13","16"]
try to use it like
{"name":"test","marks":["12","13","16"],"age":19}

Why does PHP's json_decode return a string when it should return an array?

I'm a bit confused as to why I'm not getting an array in my variable after I use json_decode(). I am using cURL to send some post data. Here is what the post data looks like for cURL
upload, post_data: Array
(
[local_file_path] => C:\videos\421F7D21-C659-43E1-9851-2397A4EEFB11.mp4
[botr_video] => "{\"path\":\"\\\/v1\\\/videos\\\/upload\",\"query\":{\"token\":\"b34f75e5c2c4a23db9850b147f0440fc536bcc7a21847e29\",\"key\":\"hKwNuueB\"},\"protocol\":\"http\",\"address\":\"upload.bitsontherun.com\"}"
)
So I call cURL, and on the receiving side of the cURL call I have the following
$upload_link=json_decode($_POST['botr_video'], true);
$file_path=$_POST['local_file_path'];
$api_format="php";
error_log('upload link data is of type: '. gettype($upload_link));
When I go check my error log, it writes this:
upload link data is of type: string
Shouldn't that be an array? The rest of the code in the receiving url depends on the json_decode to work. Am I missing something?
Also, I logged the $upload_link variable after applying the json_decode function on the $_POST['botr_video'] variable like this:
error_log('received upload link data: ' . print_r($upload_link, true));
Here is what I get in the log:
received upload link data: {"path":"\\/v1\\/videos\\/upload","query":{"token":"0835b9c46c0619cec9633a22bc9616a693696e4ea39a827c","key":"kRhvNuIF"},"protocol":"http","address":"upload.bitsontherun.com"}
Why doesn't json_decode take the post data and form an array? It seems like it's leaving it as a string.

iPhone POST JSON to PHP expecting return for login/receiving data

OK, I am quite new to this Objective-C and JSON thing, so please bare with my questions. I am using Stig Json Framework and ASIHTTPRequest.
My overall objective is to send JSON data (containing login credential) to a PHP webservice and expecting a return once the PHP process the data. I need an authorize key from the return and also specific user data tied to the login credential.
Now, I can send the JSON data via POST all right, consume the data via PHP all right. But I just don't understand the logic of the return data, as I said earlier. I only know that ASIHTTPRequest do return the response string via anything echo'ed from the PHP, but is this the correct way to get the return data I want?
Or, do I encode it back to JSON? How?
I think another viable solution is to write a webservice that receive parameters via URL and return JSON data. For example, how do I write http://search.twitter.com/search.json?q=#<name>?
The data is returned as a JSON string and you need to convert it to Objective-C object using the "JSONValue" method before working with the returned data.
For example:
NSDictionary *results = [jsonString JSONValue];
Now you can work with the returned data as an NSDictionary.
You can json_encode data from PHP and echo that. Now you will receive JSON data in the ASIHTTPRequest delegate method, that is, a response string. Then use it as you like.
Remember to set the header type to json while you echo data from PHP.

Categories