如何从Webhook获取数据以存储在数据库中?

时间:2016-08-30 15:09:33

标签: php json post sendgrid webhooks

我正在使用基于SendGrid的API v3的Webhook。 Webhook完全设置了SendGrid发布的端点,但我需要能够接收解析后的数据并使用PHP将其存储在数据库中,但不知道如何操作。

在我启动数据检索或POST之前,我使用过API,但是当API服务器是一个POSTing时,如何捕获通过Webhook端点解析的数据?到目前为止我有一些简单,但我真的不清楚如何解决这个问题:

<?php

$json = file_get_contents('https://example.com/webhook.php');
$json_decoded = json_decode($json, true);
$var_dump(json_decoded);

?>

我尝试向主机发送多封电子邮件,但每次拨打此电话时,我都会返回NULL

1 个答案:

答案 0 :(得分:2)

尝试使用this example中的代码。这将为您提供要解码的原始HTTP请求正文字节。

<?php
$data = file_get_contents("php://input");
$events = json_decode($data, true);

foreach ($events as $event) {
  // Here, you now have each event and can process them how you like
  process_event($event);
}
?>

有关php://input

的更多信息
相关问题