如何从其他Web服务捕获POST请求

时间:2018-01-30 12:26:55

标签: php json callback http-post

enter image description here

我正在使用Block.io Web Hooks。它说通知服务需要通过POST请求进行通信。它还声明所有通知事件都将使用以下JSON对象结构:

{ 
  "notification_id" : "...", // a unique identifier issued when you created the notification
  "delivery_attempt" : 1, // number of times we've tried deliverying this object
  "type" : "...", // the type of notification, helps you understand the data sent below
  "data" : {
    ... // the notification data
  },
  "created_at" : 1426104819 // timestamp of when we created this notification
}

我提供了我的回调网址,我看到有一行插入到我的数据库中,但值为空。是的我知道我的代码将插入空白但是当我触发API时它也会插入空白。

<?php
$post = '';

foreach($_POST as $k => $v){
    $post .= $k.'='.$v.'&';
}

// save data into database
?>

1 个答案:

答案 0 :(得分:1)

webhook返回json,PHP不会将其解析为$_POST数组。

相反,您需要从以下字符串获取字符串:

file_get_contents('php://input')

这可以解析自己。获得一个数组:

$array = json_decode(file_get_contents('php://input'), true);
相关问题