json_decode未使用curl调用返回正确的php api响应数组

json_decode未使用curl调用返回正确的php api响应数组,php,arrays,curl,json,Php,Arrays,Curl,Json,我正在为我的一个客户开发装运API。我有供应商提供的装运api。在以json格式发出curl请求时,json响应不会转换为php数组?找到下面的代码: $params['format'] = 'json'; $params['data'] =json_encode($package_data); $token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; $url = "http://test.shipping.co/push/json/?token="

我正在为我的一个客户开发装运API。我有供应商提供的装运api。在以json格式发出curl请求时,json响应不会转换为php数组?找到下面的代码:

$params['format'] = 'json';
$params['data'] =json_encode($package_data);
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$url = "http://test.shipping.co/push/json/?token=".$token;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
$results = json_decode($result, true);
print_r($results);
这里有2个打印功能,输出如下:

{“现金提取计数”:1.0,“现金计数”:0,“成功”:真, “包计数”:1,“上传wbn”:“UPL21969440”,“替换计数”: 0,“货到付款金额”:0.0,“预付款金额”:0,“提货数量”:0, “包裹”:[{“状态”:“成功”,“运单”:“0008110000125”, “参考编号”:“8”,“客户”:“演示”,“备注”:“cod\U金额”: 21841.0,“付款”:“现金”}],“现金提货”:21841.0}1

一,

我收到2个输出:1

我想访问此响应的php数组。我尝试了json_decode(),但它没有正确响应。
这里需要你的意见。提前谢谢。

请尝试下面的一个

$results = json_decode(file_get_contents('php://input'), true);
而不是

$results = json_decode($result, true);

问题是您没有设置
CURLOPT\u RETURNTRANSFER
选项。在这种情况下,CURL将响应直接输出到
STDOUT
(浏览器),并且
CURL\u exec
返回
true
。由于此
json\u decode
无法解码
$result
变量(因为它的值=
true

因此,您必须设置
CURLOPT\u RETURNTRANSFER
选项:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
$results = json_decode($result, true);
print_r($results);

你忘了加这个

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

这给了我同样的错误。我仍然无法获取php数组。还有其他方法可以在php中得到这个响应并处理结果吗?在添加这个之后,输出中唯一的区别是没有1作为输出。只有响应是以$results打印的。您使用的php版本是什么?$result='{“现金提货数量”:1.0,“货到付款数量”:0,“成功”:true,“包裹提货数量”:1,“上传wbn”:“UPL21969440”,“替换数量”:0,“货到付款数量”:0.0,“预付提货数量”:0,“包裹”:[{“状态”:“成功”,“运单”:“0008110000125”,“refnum”:“8”,“client”:“demo”,“comments”:“cod_amount”:21841.0,“payment”:“Cash”}],“Cash_pickups”:21841.0}’;$results=json_decode($result,true);如果没有打印出预期的输出,请进行测试。你在json_decode功能方面遇到问题了一些问题。非常感谢。你救了我一天。