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

How do I get data from Webhook to store in a database?

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

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

<?php

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

?>

我曾尝试向房东发送多封电子邮件,但每次我打电话时我 return NULL.

尝试使用 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

的更多信息