我还是webhook的新手。我需要做的是在注册平台上注册名为Bizzabo的新注册时进行回调。该平台通过让我们放置端点URL并选择将触发Webhook的操作来提供Webhook集成。我也使用了Request Bin,它可以很好地显示数据。
但是,我如何回应JSON正文数据,就像它在我的界面URL php中的Request Bin中显示的一样?
This is how the Webhook integration looks like on Bizzabo
Data captured from Webhook when tested using Request Bin
谢谢!
答案 0 :(得分:1)
您需要一个接收回调的端点,而不是Request Bin,然后使用file_get_contents('php://input')
和json_decode()
例如http://example.com/bizzabo-callback-handler.php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// fetch RAW input
$json = file_get_contents('php://input');
// decode json
$object = json_decode($json);
// expecting valid json
if (json_last_error() !== JSON_ERROR_NONE) {
die(header('HTTP/1.0 415 Unsupported Media Type'));
}
/**
* Do something with object, structure will be like:
* $object->accountId
* $object->details->items[0]['contactName']
*/
// dump to file so you can see
file_put_contents('callback.test.txt', print_r($object, true));
}
答案 1 :(得分:0)
如果以压缩格式(gzip)接收数据,请使用gzdecode
:
<?php
if (!function_exists('gzdecode')){
function gzdecode($data){
// strip header and footer and inflate
return gzinflate(substr($data, 10, -8));
}
}
// get compressed (gzip) POST request into a string
$comprReq = file_get_contents('php://input');
// get decompressed POST request
$decomprReq = gzdecode($comprReq);
// decode to json
$jsonData = json_decode($decomprReq, true);
// do your processing on $jsonData
?>