PHP POST method json_decode return NULL - php

I'm using sencha touch and I'm sending data to php REST server to save it to database, in firebug I can see the parameters that sencha touch send to php side, but in php I have this code:
parse_str(file_get_contents("php://input"),$post_vars);
$info=$post_vars['customers'];
$data=json_decode(stripslashes($info),true);
The json_decode return NULL, the get_magic_quotes_gpc is off I also tried utf8_encode but always I got NULL, I tried var_dump and at the response I got extra text:
array(1) {
["customers"]=>
string(50) "{"c_name":"test","c_tel":"08-05852821","id":"112"}"
}
I don't know how to continue, before the var_dump the post contains:
{"success":{"customers":"{\"c_name\":\"test\",\"c_tel\":\"08-05852821\",\"id\":\"112\"}"}}
I tried stripslashes but I got also NULL...
Any idea...

Based on your comment, I would access $_POST directly:
$info = json_decode($_POST['customers'], true);
echo $info['c_name'];

There are three possibilities how Jason can be transmitted.
First, it can be a get variable with a Name (index.php?data={success:true}), this only Works for Short json strings.
Second, it can be a post variable with a Name ( will be Sent as x-www-form-urlencoded)
Third, you can post only json (Store.sync does that)
Only the Latter will be accessed via php://Input, it Seems as of you use the Second.

Related

Reading JSON from HTTP POST using PHP (Typeform)

I'm having trouble reading JSON data (using PHP) that's coming in from a webhook (Typeform.)
I've tried the solution provided Here but I'm still getting a null array. My code is exactly the same as the example shown in the link (although I'm not accessing particular arrays, I just want to read the data.) All my test deliveries are returning 200, so I'm just a bit stuck.
$data = json_decode(file_get_contents('php://input'));
var_dump($data);
TypeForm runs the page, They'll get the var_dump response...
When you run the page, you haven't posted anything so $data is empty, therefore null
Try saving $data to a file, then when TypeForm POSTS to it, You can see what the POST contents were, in that file
file_put_contents("webhookData.txt",$data, FILE_APPEND);

How to return a response via php

I'm writing a php api with methods wired together by php routing, such that
dev.myphp.com/user/login?email=sdfsdf#fdfd.com&password=d514
will return a json/xml response.
However, I do not know how to return this response, even if I use json_encode or xml_encode to conver the data string.
Do I just need to uniquely echo the JSON string out?
Always try to take data using POST method not GET , untill unless you don't find it less useful in caompare to POSt.
use simply
<?Php
--- php code
return json_encode($resultdata);
?>
it would be enough.

AS3 to PHP, JSON type error? C/Ping the string works, $_posting the JSON itself doesn't

I'm passing a JSON object from flash AS3 to PHP, which is then taken apart and passed to the DB.
In Flash:
var jsonObject:Object = JSON.encode(currentlySelectedArray);
In PHP:
$json_pieces_array = $_POST['jsonArray'];
$json_obj = json_decode($json_pieces_array, true);
When I test my code by copy/pasting the output of 'trace (saveDataJSON.ToString());' and putting it into my '$_POST['jsonArray'] = '[[Valid JSONLint checked JSON here.]]', everything works fine and it gets pushed to the database.
But when I don't meddle and use the flash-sent $_POST, nothing pushes to the MYSQL DB.
My question is twofold:
1) What's the best way to bug test this sort of complication? I'm in the Flash interface.
2) What sorts of things should I be looking for? I already checked that the JSON being encoded was valid. Is there some sort of weird typecasting I'm missing?
At first you have to debug request from flash. You can do it in several ways:
create dummy file that does var_dump($_REQUEST);;
add file_put_contents('my_dump_file.txt', var_export($_REQUEST, true)); in existing script;
check your webserver logs;
debug you script with debugger (xdebug or similar).
Then you should check your PHP script. Try to var_dump($_REQUEST); on the top and exactly before json_decode. It can be so that $_POST['jsonArray'] is overwritten somewhere else.
I guess that the problem is between flash and php. In most cases there is simply misspelling or $_POST is mixed with $_GET.

AJAX json data does not print in PHP

i have a JSON data in Javascript made form array using JSON.stringify
{
"user":"Mark",
"id":"80",
"0":["age","=","twenty four","varchar"],
"1":["prefix","=","Mr.","enum"]
}
i am sending this via AJAX to PHP file. When i echo the POST i get the values
echo (serialize($_POST['data']));
s:263:
"{
"user":"Mark",
"id":"80",
"0":["age","=","twenty four","varchar"],
"1":["prefix","=","Mr.","enum"]
}";
How can i get the POSTed data in an Array or Object. i have tried to do
var_dump(json_decode($_POST['data']));
AND
var_dump(json_decode(serialize($_POST['data'])));
AND
var_dump(json_decode($_POST['data'],true));
but they did not work. Output is null.
json_decode() should do the trick for you, but depending on your server config (if magic_quotes_gpc is on), you might need to use stripslashes() before decoding.
If your PHP versopn >=5.2.0, you can use following build in PHP functions to decode JSONS
json_decode($_POST['data'])
It return Array and StdClass object.
Edit: How did you found json_decode is not working. Please try ver_dump or print_r. Hoping your PHP version >=5.2.0
You have to store it to something.
$posted = json_decode($_POST['data']);
var_dump($posted);

Access PUT-data in PHP

I want to access JSON data sent with the PUT-method, as a payload to a PHP-backend.
From what I've read, the data should be available in the php://input stream, and I should be able to fetch it using.
file_get_contents('php://input');
This works for POST-requests, but just returns an empty string when using PUT,
although I can find the PUT-data as the first key in the $_POST variable, like this:
array(
"{"latLngPosition":null,"pixelPosition":null,"id":"1","marking":"012312312313"}" => ""
)
It doesn't seem right to fetch the data this way, so I wonder what I might do wrong when fetching using php://input.
I'm using nginx with fastcgi to run PHP.

Categories