Liu Chuyu Liu Chuyu - 2 months ago 10
PHP Question

how to write a php to get the json post from a webhook?

I add my url xxx/getjson.php to a webhook and once a person signup, it will post the json data to my url. I use http://requestb.in/ to check the data and the result is like this:

payload{ "signup": { "address": { "address1": "XX",
"country": "United States"},
"id":22}}
token


the php script i write is:

$obj=json_decode($_POST); //cannot get the json data

$userid=$obj->signup->id;


Also I don't know how to use the 'payload'

I find a similar sample code and I test it well using their web hooks.
http://support.unbounce.com/entries/307685-how-does-the-form-webhook-work
However, they use 'data.json' rather than 'payload' as parameters.

$form_data = json_decode($unescaped_post_data['data_json']);
$userid= =$form_data->signup->id;


I used their stripslashes_deep function, and replaced the 'data_json' with 'payload' but still doesn't work.

I really appreciate your help.Thanks!

Answer

$_POST will be an array so you need to specify the key.

$obj=json_decode($_POST['payload']); // put the second parameter as true if you want it to be a associative array

$userid=$obj->signup->id;
Comments