6

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!

Chuyu
  • 93
  • 1
  • 1
  • 6
  • 1
    What is the post var? `$_POST['payload']` –  Jun 11 '12 at 21:27
  • 1
    possible duplicate of [php://input <> $_POST?](http://stackoverflow.com/questions/4703906/php-input-post) or http://stackoverflow.com/questions/5806971/read-associative-array-from-json-in-post - depending on specifica not mentioned in your question. – mario Jun 11 '12 at 21:30
  • DO we need to make any changes in the php or apache configuration to make this work. I am using the sendgrid event webhooks but it is not posting to my php file. – Happy Coder Sep 20 '13 at 07:09

3 Answers3

3

$_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;
3

Finally worked it out! Only three lines needed but I spent the whole day... The webhook API provider should provide some more information about it. Thanks for your help!

$data = $_REQUEST["payload"];           
$unescaped_data = stripslashes($data);
$obj = json_decode($unescaped_data);
$userid = $obj->signup->id;
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
Chuyu
  • 93
  • 1
  • 1
  • 6
0

it looks like $obj=json_decode($_POST); is failing to decode your JSON string.

I think that the problem you are having is that you are not properly JSON encoding your "payload" data.

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

token is not formatted properly. { "signup": { "address": { "address1": "XX","country": "United States"},"id":22}} is the correct JSON string. the extra stuff on either end of the JSON will cause it to not be parsable.

$_POST['payload'] would probably be how you access it.

Also, you didnt give us enough code to really help. we need either the HTML form, or the Javascript that is actually sending/building the POST. (or are you using something like Curl?)

Community
  • 1
  • 1
Francis Yaconiello
  • 10,829
  • 2
  • 35
  • 54
  • 1
    only works when I use $_REQUEST["payload"] rather than $_POST. The webhook api provider doesn't provide me much information. Now I figure out 'payload' is a variable. Thanks – Chuyu Jun 12 '12 at 14:35