I'm trying to parse JSON sent by POST to my webservice. I'm using Advanced Rest Client tool for Google Chrome to test restapi.
How can I read this request and response to it?
I'm sending key called "format" and "json" as value for this key. I'm adding JSON like
"{"id":"235325"}"
Part of my PHP API code:
if( strcasecmp($format,'json') == 0 )
{
//how to read that id = 235325?
}
vhu
12.9k11 gold badges42 silver badges50 bronze badges
-
can you past the content of var_dump($_POST)?mdamia– mdamia2015年08月20日 18:17:57 +00:00Commented Aug 20, 2015 at 18:17
-
but how? I'using Google Chrome plugin called Advanced Rest Client. And i'am waiting for response. i dont kwno how to attach this json: "{"id":"235325"}" to link: 78.28.23.180/index2.php?format=json on webbrowserkmathew– kmathew2015年08月20日 18:23:07 +00:00Commented Aug 20, 2015 at 18:23
-
the plugin may not support post data, use curl from your application and post data a to file expecting post. return the result.mdamia– mdamia2015年08月20日 18:43:27 +00:00Commented Aug 20, 2015 at 18:43
-
@mdamia the problem is not getting the data to the server, but how to handle it once it is Inside the serverlitelite– litelite2015年08月20日 19:38:18 +00:00Commented Aug 20, 2015 at 19:38
2 Answers 2
Try the json_decode()
function. It is the standard function to parse json in php.
answered Aug 20, 2015 at 19:36
1 Comment
Ryan Vincent
Would it help the OP, and other viewers of your answer, to explain why this function applies to this problem? And a visit to the manual will be worthwhile?
If want to work with array:
$json = '{"id":"235325"}';
$array = json_decode($json, true);
foreach($array as $element){
if($element == 0){
}
}
With object:
$json = '{"id":"235325"}';
$object = json_decode($json);
if($object->id == 0){
}
answered Aug 20, 2015 at 20:59
Comments
lang-php