[英]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. 我将我的url xxx / getjson.php添加到webhook,一旦一个人注册,它会将json数据发布到我的url。 I use http://requestb.in/ to check the data and the result is like this:
我使用http://requestb.in/检查数据,结果如下:
payload{ "signup": { "address": { "address1": "XX",
"country": "United States"},
"id":22}}
token
the php script i write is: 我写的php脚本是:
$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. 我找到了一个类似的示例代码,我使用他们的Web钩子测试它。 http://support.unbounce.com/entries/307685-how-does-the-form-webhook-work However, they use 'data.json' rather than 'payload' as parameters.
http://support.unbounce.com/entries/307685-how-does-the-form-webhook-work然而,他们使用'data.json'而不是'payload'作为参数。
$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. 我使用了他们的stripslashes_deep函数,并将'data_json'替换为'payload'但仍然不起作用。
I really appreciate your help.Thanks! 非常感谢你的帮助。谢谢!
$_POST will be an array so you need to specify the key. $ _POST将是一个数组,因此您需要指定密钥。
$obj=json_decode($_POST['payload']); // put the second parameter as true if you want it to be a associative array
$userid=$obj->signup->id;
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.
只需要三行,但我花了一整天...... webhook API提供商应提供更多有关它的信息。 Thanks for your help!
谢谢你的帮助!
$data = $_REQUEST["payload"];
$unescaped_data = stripslashes($data);
$obj = json_decode($unescaped_data);
$userid = $obj->signup->id;
it looks like $obj=json_decode($_POST);
它看起来像
$obj=json_decode($_POST);
is failing to decode your JSON string. 无法解码您的JSON字符串。
I think that the problem you are having is that you are not properly JSON encoding your "payload" data. 我认为你遇到的问题是你没有正确地对你的“有效载荷”数据进行JSON编码。
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. { "signup": { "address": { "address1": "XX","country": "United States"},"id":22}}
是正确的JSON字符串。 the extra stuff on either end of the JSON will cause it to not be parsable. JSON两端的额外内容将导致它无法解析。
$_POST['payload']
would probably be how you access it. $_POST['payload']
可能就是你访问它的方式。
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.
我们需要HTML表单,或者实际发送/构建POST的Javascript。 (or are you using something like Curl?)
(或者你正在使用像Curl这样的东西?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.