duanli9930
2016-10-18 12:48
浏览 159
已采纳

PHP获取JSON POST数据

I've setup a Webhook to get notifications posted to a PHP page on my server. The notification request to my server is like this:

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

I've tried using the following to convert the JSON data to a string that I can work with, but I'm getting nothing so far:

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

Everything I've read indicates this should work - I tested the following and this is actually working:

$webhookContent = "";

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

I'm trying to understand why file_get_contents('php://input'); doesn't work when everything I've read indicates that's the function I should be using, and why fopen('php://input' , 'rb'); works instead?

If I do var_dump($inputJSON) I get:

    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) returns NULL

图片转代码服务由CSDN问答提供 功能建议

我已经设置了一个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”:“感谢发短信,这是一个自动回复!”,
“bodySize”:42,
“可见”:true,
“hasAttachment”:false  ,
“dateRead”:null,
“bcc”:null,
“finalDestination”:“4257772300”,
“messageType”:“MO”,
“已删除”: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 = fo  pen('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“:” 感谢发短信,这是一个自动回复!“,
”bodySize“:42,
”可见“:true,
”hasAttachment“:false,
”dateRead“:null,
”bcc“:  null,
“finalDestination”:“4257772300”,
“messageType”:“MO”,
“已删除”: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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanchi0883649 2016-11-07 09:16
    已采纳

    The following is working for me now:

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

    I think my issue was using:

    $input= json_decode( $inputJSON, TRUE ); 
    

    instead of just:

    $input= json_decode( $inputJSON ); 
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题