AJAX json data does not print in PHP - 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);

Related

PHP POST method json_decode return NULL

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.

Take variables from a PHP script

I am making a website to display the data at https://api.captcoin.com/address/top/100. I need to be able to make the website take variables("address", "percent", "balance", and "rank") from this script and make them local variables in my site so I can display them. How can I take these variables and use them in my site?
First you need to get the remote page contents:
$remote = file_get_contents('link');
Then, since the data is in json format, you need to decode it using json_decode function.
$data = json_decode($remote, true);
true means that $remote should be decoded as associative array.
And finally you can access the data like an ordinary php array:
echo $data['top'][0]['address'];
Also, you should add some logic to handle situations when remote server is not accessible.
Use json_decode to convert the content of that url into an array and then search through it like you would through any array.
To get the actual content of the site please refer to this post Get file content from a URL?
You can either do it with javascript or php.
With javascript use this:
http://api.jquery.com/jquery.getjson/
You take the pages output and push them as variables to the php.
With php you can use
http://php.net/manual/en/function.json-decode.php
You make an array to push the json data into:
$object = array();
$json = file_get_contents('https://api.captcoin.com/address/top/100');
$object = json_decode ( string $json , true)
Be aware this is untested and read the json_decode api to customize the function-call.

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.

Show PHP echo results when called by JSON rpc?

I am using JSON to run some PHP functions and 1 of them is not working and is returning null. How can i return a php output e.g. an echo of variable/array from the php function and view in firebug/on screen? I'm sure i have done this before but cant remember how!?
Thanks
Paul
echo json_encode(array("hello" => "world"))
Will output a JSON encoded array, you can then access this script via an Ajax call, Firebug can view this and you can debug from there?
I used this to pass POST and GET arrays into json, so:
$var = <?php json_encode($_POST); ?>
So if i have $_POST['test'] = 'what'
to access it i do $var.test which is equal to 'what'

Categories