使用 json 网络书 php
working with json webook php
我在接收 webhook 数据时遇到问题。
Wobhook 数据以 JSON 格式发送,如下所示:
{
"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 毫秒,错误:位置 0JSON 中的意外标记 <
<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">
Home
stdClass 对象 ([event_name] => chat_finished [chat_id] => 5302 [widget_id] => 9IpBqsF7Mt [visitor] => stdClass Object ( [name] => v [number] => 106597 [chats_count] => 2 ) [chat] => stdClass Object ( [messages] => Array ( [ 0] => stdClass 对象 ( [timestamp] => 1500275477 [type] => visitor [message] => test ) [1] => stdClass Object ( [timestamp] => 1500275823 [type] => visitor [message] = > 测试)[2] => stdClass 对象([时间戳] => 1500275896 [类型] => 访问者[消息] => 测试)[3] => stdClass 对象([时间戳] => 1500276056 [类型] => 访问者[message] => test ) ) [rate] => ) [agents] => Array ( [0] => stdClass Object ( [id] => 354276 ) [session] => stdClass Object ( [geoip] => stdClass对象 ( [region_code] => 26 [country_code] => UA [国家] => 乌克兰 [地区] => Zaporiz'ka Oblast' [城市] => Zaporizhzhya ) [utm] => [user_agent] => Mozilla/5.0(Windows NT 6.1;Win64;x64)AppleWebKit/537.36(KHTML,如 Gecko)Chrome/59.0.3071。 115Safari/537.36 )) © 2017
但是我没有在屏幕上看到任何文本,所有数组都是空的:
NULL array(0) { } array(0) { } array(0) { }
我做错了什么?提前致谢。
因为你 运行 json_decode() 两次
试试这个
$contents = file_get_contents("php://input");
$data = json_decode($contents, true);
var_dump($data);
我在接收 webhook 数据时遇到问题。 Wobhook 数据以 JSON 格式发送,如下所示:
{
"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 毫秒,错误:位置 0JSON 中的意外标记 <
<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">
Home
stdClass 对象 ([event_name] => chat_finished [chat_id] => 5302 [widget_id] => 9IpBqsF7Mt [visitor] => stdClass Object ( [name] => v [number] => 106597 [chats_count] => 2 ) [chat] => stdClass Object ( [messages] => Array ( [ 0] => stdClass 对象 ( [timestamp] => 1500275477 [type] => visitor [message] => test ) [1] => stdClass Object ( [timestamp] => 1500275823 [type] => visitor [message] = > 测试)[2] => stdClass 对象([时间戳] => 1500275896 [类型] => 访问者[消息] => 测试)[3] => stdClass 对象([时间戳] => 1500276056 [类型] => 访问者[message] => test ) ) [rate] => ) [agents] => Array ( [0] => stdClass Object ( [id] => 354276 ) [session] => stdClass Object ( [geoip] => stdClass对象 ( [region_code] => 26 [country_code] => UA [国家] => 乌克兰 [地区] => Zaporiz'ka Oblast' [城市] => Zaporizhzhya ) [utm] => [user_agent] => Mozilla/5.0(Windows NT 6.1;Win64;x64)AppleWebKit/537.36(KHTML,如 Gecko)Chrome/59.0.3071。 115Safari/537.36 )) © 2017但是我没有在屏幕上看到任何文本,所有数组都是空的:
NULL array(0) { } array(0) { } array(0) { }
我做错了什么?提前致谢。
因为你 运行 json_decode() 两次 试试这个
$contents = file_get_contents("php://input");
$data = json_decode($contents, true);
var_dump($data);