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

标签 php json post sendgrid webhooks

我正在使用基于 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/

相关文章:

php - 在 CodeIgniter View 中的 <object> 标签中显示 PDF

php - MongoDB/NoSQL 性能和设计

php - SQL Buddy + https(ssl) + nginx 无法登录

javascript - 使用适配器和序列化器通过版本 2 WP REST API 将 Ember 2.3 应用程序连接到 WordPress 后端

php - 代理后面的 curl 发布文件返回错误

php - 插入查询不起作用,如果和其他两个循环都在执行

php - java.lang.String类型的值成功无法转换为JSONObject

json - 在 powershell 中遍历巨大的 JSON

http - CherryPy 3.6 - 读取 Multipart Post http 请求

jquery - ajax表单提交中如何提交表单名称和提交按钮名称