【问题标题】:PHP Get JSON POST DataPHP 获取 JSON POST 数据
【发布时间】:2016-10-18 12:48:12
【问题描述】:

我设置了一个 Webhook 来获取发布到我服务器上 PHP 页面的通知。对我的服务器的通知请求是这样的:

POST /message/receive HTTP/1.1
Host: http://www.yoururl.com/zipwhip/api/receive
Content-Length: 581
Content-Type: application/json; charset=UTF-8

{ "body":"Thanks for texting, this is an auto reply!",
  "bodySize":42,
  "visible":true,
  "hasAttachment":false,
  "dateRead":null,
  "bcc":null,
  "finalDestination":"4257772300",
  "messageType":"MO",
  "deleted":false,
"statusCode":4,
"id":634151298329219072, "scheduledDate":null, "fingerprint":"132131532", "messageTransport":9, "contactId":3382213402, "address":"ptn:/4257772222",
"read" "dateCreated":"2015-08-19T16:53:45-07:00", "dateDeleted":null,
  "dateDelivered":null,
  "cc":null,
  "finalSource":"4257772222",
} "dev

我尝试使用以下方法将 JSON 数据转换为我可以使用的字符串,但到目前为止我什么也没得到:

$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON, TRUE ); 

我读过的所有内容都表明这应该有效 - 我测试了以下内容,这实际上有效:

$webhookContent = "";

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

我试图理解为什么 file_get_contents('php://input');当我阅读的所有内容都表明这是我应该使用的功能以及为什么 fopen('php://input' , 'rb'); 时不起作用可以代替吗?

如果我执行 var_dump($inputJSON) 我会得到:

    string(527) "{ "body":"Thanks for texting, this is an auto reply!",
  "bodySize":42,
  "visible":true,
  "hasAttachment":false,
  "dateRead":null,
  "bcc":null,
  "finalDestination":"4257772300",
  "messageType":"MO",
  "deleted":false,
"statusCode":4,
"id":634151298329219072, "scheduledDate":null, "fingerprint":"132131532", "messageTransport":9, "contactId":3382213402, "address":"ptn:/4257772222",
"read" "dateCreated":"2015-08-19T16:53:45-07:00", "dateDeleted":null,
  "dateDelivered":null,
  "cc":null,
  "finalSource":"4257772222",
}"

var_dump($input) 返回 NULL

【问题讨论】:

  • 那么什么不起作用,var_dump($inputJSON) 给你什么?
  • 您的 JSON 在我看来无效。
  • @jeroen 我已经编辑了问题以包含 var_dump($inputJSON) 的结果
  • 那不是有效的 json。你是如何创建它的?
  • 好像是序列化数据而不是json数据,试试php的serialize()和unserialize(),而不是json_****()函数...

标签: php json http


【解决方案1】:

以下内容现在对我有用:

$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON ); 

我认为我的问题是使用:

$input= json_decode( $inputJSON, TRUE ); 

而不仅仅是:

$input= json_decode( $inputJSON ); 

【讨论】:

    猜你喜欢
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多