使用json的webook php

问题描述:

我有一个接收webhook数据的问题。 Wobhook数据以JSON格式发送这样的:使用json的webook php

{ 
"event_name": "chat_finished", 
"chat_id": 5302, 
"widget_id": "9IpBqsF7Mt", 
"visitor": { 
    "name": "v", 
    "email": "", 
    "phone": "", 
    "number": 106597, 
    "chats_count": 2 
}, 
"chat": { 
    "messages": [ 
     { 
      "timestamp": 1500275477, 
      "type": "visitor", 
      "message": "<Message text is not displayed here>" 
     }, 
     { 
      "timestamp": 1500275823, 
      "type": "visitor", 
      "message": "<Message text is not displayed here>" 
     }, 
     { 
      "timestamp": 1500275896, 
      "type": "visitor", 
      "message": "<Message text is not displayed here>" 
     }, 
     { 
      "timestamp": 1500276056, 
      "type": "visitor", 
      "message": "<Message text is not displayed here>" 
     } 
    ], 
    "rate": null 
}, 
"session": { 
    "geoip": { 
     "region_code": "26", 
     "country_code": "UA", 
     "country": "Ukraine", 
     "region": "Zaporiz'ka Oblast'", 
     "city": "Zaporizhzhya", 
     "isp": "", 
    }, 
    "utm": null, 
    "user_agent": "Mozilla\/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/59.0.3071.115 Safari\/537.36" 
} 

}

在我的PHP脚本我有这样的代码:

$data = json_decode(file_get_contents("php://input"),true); 
$data = json_decode($data,true); 
var_dump($data); 
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); 
fwrite($myfile, $data); 
fclose($myfile); 
var_dump($_GET); 
var_dump($_POST); 
var_dump($_REQUEST); 

在网络API日志文件中我看到,我的代码以某种方式获取webhook:

响应代码:200,持续时间:265毫秒,错误:意外的令牌< JSON在位置0

<html> <head> <title>CodeIgniter Tutorial</title> <link rel = "stylesheet" type = "text/css" href = "http://crm.t.zp.ua/css/crm-t-zp-ua-1.0.0.css"> 

http://crm.t.zp.ua/js/jquery-3.2.1.min.js“> http://crm.t.zp.ua/js/crm-t- zp-ua-1.0.0.js“>

首页

stdClass对象([event_name] => chat_finished [chat_id] => 5302 [widget_id] => 9IpBqsF7Mt [visitor] => stdClass Object([name] => v [数字] => 106597 [chats_count] => 2)[chat] => stdClass Object([messages] => Array([0] => stdClass Object([timestamp] => 1500275477 [type] => visitor [message] => test] [1] => stdClass Object([timestamp] => 1500275823 [type] => visitor [message] => test)[2] => stdClass Object([timestamp] => 1500275896 [type] =>访客[message] => test)[3] => stdClass Object([timestamp] => 1500276056 [type] => visitor [message] => test))[rate] =>)[agents] => Array([ 0] => stdClass Object([id] => 354276)[sessi on] => stdClass Object([geoip] => stdClass Object([region_code] => 26 [country_code] => UA [country] =>乌克兰[region] => Zaporiz'ka Oblast'[city] => Zaporizhzhya) [utm] => [user_agent] => Mozilla/5.0(Windows NT 6.1; Win64平台; 64)为AppleWebKit/537.36(KHTML,例如Gecko)Chrome浏览器/ Safari浏览器59.0.3071.115/537.36)) © 2017年

但是我不明白屏幕上的任何文字和所有的数组都是空的:

NULL array(0) { } array(0) { } array(0) { } 

我该怎么做?感谢advnce。

,因为你运行json_decode()两次 试试这个

$contents = file_get_contents("php://input"); 
$data = json_decode($contents, true); 
var_dump($data); 
+0

感谢您指出我的错误。 –