Webhooks:file_get_contents为空

时间:2018-11-28 14:47:02

标签: php json

我正在尝试学习如何使用网络挂钩。我有一个带有ngrok的本地主机环境,以便为我的webhook回调URL获取https链接。但是我在接收Webhook数据时遇到问题。 这是我用来获取Webhook数据的代码:

<?php
require __DIR__ . '/vendor/autoload.php';

$webhook = file_get_contents('php://input');
$webhook_array = json_decode($webhook, true);
var_dump($webhook_array);
http_response_code(200);

echo $webhook_array->Content->Topic;
if($webhook_array->Content->Action == "Update"){
  echo $event->data->object->id;
}

?>

因为我正在使用ngrok,所以我可以看到正在发布到我的php文件中的内容,它看起来像这样:

{
    "Content": {
        "Topic": "Items",
        "ClientId": "423de8c8-409e-46f2-8d57-dc7eab60b747",
        "Division": 2286072,
        "Action": "Update",
        "Key": "90197dc4-34ca-4627-af77-288ea5c3cdd6",
        "ExactOnlineEndpoint": "123412341234')",
        "EventCreatedOn": "2018-11-28T15:13:56.607"
    },
    "HashCode": "1234123412341234"
}

我的php代码响应如下:

NULL
<br />
<b>Notice</b>:  Trying to get property 'Content' of non-object in <b>C:\xampp\htdocs\exact\webhooks.php</b> on line <b>11</b><br />
<br />
<b>Notice</b>:  Trying to get property 'Topic' of non-object in <b>C:\xampp\htdocs\exact\webhooks.php</b> on line <b>11</b><br />
<br />
<b>Notice</b>:  Trying to get property 'Content' of non-object in <b>C:\xampp\htdocs\exact\webhooks.php</b> on line <b>12</b><br />
<br />
<b>Notice</b>:  Trying to get property 'Action' of non-object in <b>C:\xampp\htdocs\exact\webhooks.php</b> on line <b>12</b><br />

NULL来自var_dump,由于$ webhook_array为空,因此尝试获取属性错误。因此,主要问题是file_get_contents无法从帖子中获取数据。 我该如何解决?或者我该怎么做才能使用file_get_contents获取数据?

0 个答案:

没有答案