我正在使用基于 SendGrid API v3 的 Webhook。 Webhook 完全设置有 SendGrid 发布到的端点,但我需要能够接收已解析的数据并将其存储在使用 PHP 的数据库中,但不知道如何执行此操作。
我在启动数据检索或 POST 之前使用过 API,但是当 API 服务器是一个 POST 时,我如何捕获通过 Webhook 端点解析的数据?到目前为止我有一些简单的,但我真的不清楚如何解决这个问题:
<?php
$json = file_get_contents('https://example.com/webhook.php');
$json_decoded = json_decode($json, true);
$var_dump(json_decoded);
?>
我曾尝试向主机发送多封电子邮件,但每次调用时我都会返回 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
的更多信息
关于php - 如何从 Webhook 获取数据以存储在数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39231165/