【发布时间】:2013-05-09 23:52:27
【问题描述】:
我正在处理JSON 使用cURL 的响应,但是它只是返回NULL。我不明白我在哪里做错了?
这是我的代码:
<?php
$data = array('name' => 'Hagrid','age' => '36');
$bodyData = array('json' => json_encode($data));
$bodyStr = http_build_query($bodyData);
$url = 'http://bulk10.co.in/curl/index.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded','Content-Length: '.strlen($bodyStr)));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyStr);
$result = curl_exec($ch);
echo $result;
?>
index.php:
<?php
$order_info = json_decode($_POST['json']);
var_dump($order_info);
?>
谁能帮助我为什么我收到NULL 回复?
【问题讨论】:
-
你在
$_POST['json'](没有json_decode)上得到了什么?来自documentation:如果无法解码 json 或编码数据深度超过递归限制,则返回 NULL。 -
不,它只是显示为空...
-
抱歉,一切正常,我刚刚在本地主机上进行了测试。您的网络服务器或网络缓存应该有错误配置。
-
你得到了什么输出?
-
object(stdClass)[1] public 'name' => string 'Hagrid' (length=6) public 'age' => string '36' (length=2)onecho $result;