gpt4 book ai didi

php - 使用 PHP 转换 WooCommerce Webhook 有效负载

转载 作者:行者123 更新时间:2023-12-03 09:13:21 25 4
gpt4 key购买 nike

我已经使用 WooCommerce 设置了我的第一个 Webhook,以便在下新订单时通知我的数据库服务器。 Webhook 已设置并由我的服务器发送/接收 - 我不想编写一个 PHP 页面来解析 Webhook 的有效负载,以便我可以将带有订单详细信息的新记录写入我的 CRM 数据库。

我在获取 Webhook 的内容时遇到问题。根据 WooCommerce 内部 Webhook 的日志,请求详细信息为(我缩写了内容 JSON 数据):

Headers:

user-agent: WooCommerce/2.6.4 Hookshot (WordPress/4.6.1)
content-type: application/json
x-wc-webhook-source: http://example.com/
x-wc-webhook-topic: order.created
x-wc-webhook-resource: order
x-wc-webhook-event: created
x-wc-webhook-signature: xxxxxxxxxxxxxxxxxxx=
x-wc-webhook-id: 3233
x-wc-webhook-delivery-id: 94

Content:

{"order":{"id":3242,"order_number":3242,"order_key":"wc_order_57f1dbe5bcf03","created_at":"2016-10-03T04:17:41Z", .....

它正在执行 POST 请求,但我无法检索 POST 数据,例如

$postData = var_export($_POST, true);       
error_log($postData, 0);

不返回任何 JSON 数据。我正在寻求以下方面的帮助:

  1. 检索 Webhook 在 POST 请求中发送的 JSON 数据
  2. 帮助了解如何解析 JSON 的每个元素以插入适当的数据库字段

最佳答案

这最终对我有用:

$webhookContent = "";

$webhook = fopen('php://input' , 'rb');
while (!feof($webhook)) {
$webhookContent .= fread($webhook, 4096);
}
fclose($webhook);

关于php - 使用 PHP 转换 WooCommerce Webhook 有效负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39825053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com