【问题标题】:json_decode is not returning proper php array of api response with curl calljson_decode 没有通过 curl 调用返回正确的 php api 响应数组
【发布时间】:2014-10-03 21:02:04
【问题描述】:

我正在为我的一位客户开发运输 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 个 print_r 函数,输出如下:

{ "cash_pickups_count": 1.0, "cod_count": 0, "success": true, “package_count”:1,“upload_wbn”:“UPL21969440”,“replacement_count”: 0,“cod_amount”:0.0,“prepaid_count”:0,“pickups_count”:0, “包裹”:[{“状态”:“成功”,“运单”:“0008110000125”, “refnum”:“8”,“client”:“demo”,“remarks”:“”,“cod_amount”: 21841.0,“付款”:“现金”}],“cash_pickups”:21841.0 }1

1

我收到 2 个输出:1

我想访问这个响应的 php 中的数组。我试过 json_decode() 但它没有正确响应。 在这里需要您的输入。提前致谢。

【问题讨论】:

    标签: php arrays curl json


    【解决方案1】:

    问题是你没有设置CURLOPT_RETURNTRANSFER 选项。在这种情况下,CURL 将响应直接输出到STDOUT(浏览器),curl_exec 返回true。因此,json_decode 无法解码 $result 变量(因为它的值 = true)。

    所以你必须设置CURLOPT_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);
    

    【讨论】:

    • 太棒了...它奏效了。非常感谢。你拯救了我的一天。
    【解决方案2】:

    你忘了添加这个

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    

    【讨论】:

      【解决方案3】:

      请在下面试试这个

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

      而不是

      $results = json_decode($result, true);
      

      【讨论】:

      • 它给了我同样的错误。我仍然无法获取 php 数组。还有什么其他方法可以让我在 php 中得到这个响应并处理结果?
      • 添加后,输出的唯一区别是 1 作为输出没有出现。只有响应打印在 $results 中。
      • 你用的是什么php版本?
      • $result = '{ "cash_pickups_count": 1.0, "cod_count": 0, "success": true, "package_count": 1, "upload_wbn": "UPL21969440", "replacement_count": 0 , "cod_amount": 0.0, "prepaid_count": 0, "pickups_count": 0, "packages": [ { "status": "Success", "waybill": "0008110000125", "refnum": "8", "客户”:“演示”,“备注”:“”,“cod_amount”:21841.0,“付款”:“现金”}],“cash_pickups”:21841.0 }'; $results = json_decode($result, true);如果它没有打印所需的输出,请测试这个,你可能在 json_decode 函数中遇到问题
      猜你喜欢
      • 1970-01-01
      • 2019-09-24
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2013-06-25
      • 2013-09-22
      • 2019-10-28
      • 1970-01-01
      相关资源
      最近更新 更多