PHP decrypts JSON POST

I am trying to get POST data in JSON form. I twist it like:

 curl -v --header 'content-type:application/json' -X POST --data '{"content":"test content","friends":[\"38383\",\"38282\",\"38389\"],"newFriends":0,"expires":"5-20-2013","region":"35-28"}' http://testserver.com/wg/create.php?action=post 

On the PHP side, my code is:

 $data = json_decode(file_get_contents('php://input')); $content = $data->{'content'}; $friends = $data->{'friends'}; // JSON array of FB IDs $newFriends = $data->{'newFriends'}; $expires = $data->{'expires'}; $region = $data->{'region'}; 

But even when I print_r ( $data) , nothing returns to me. Is this the correct way to handle POST without a form?

+8
json php
source share
1 answer

The JSON data you submit is invalid JSON.

When you use "in its shell, it will not process", as you suspect.

 curl -v --header 'content-type:application/json' -X POST --data '{"content":"test content","friends": ["38383","38282","38389"],"newFriends":0,"expires":"5-20-2013","region":"35-28"}' 

Works as expected.

 <?php $foo = file_get_contents("php://input"); var_dump(json_decode($foo, true)); ?> 

Outputs:

 array(5) { ["content"]=> string(12) "test content" ["friends"]=> array(3) { [0]=> string(5) "38383" [1]=> string(5) "38282" [2]=> string(5) "38389" } ["newFriends"]=> int(0) ["expires"]=> string(9) "5-20-2013" ["region"]=> string(5) "35-28" } 
+20
source share

All Articles