【问题标题】:Dealing with JSON Response in cURL在 cURL 中处理 JSON 响应
【发布时间】: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' =&gt; string 'Hagrid' (length=6) public 'age' =&gt; string '36' (length=2) on echo $result;

标签: php json curl null


【解决方案1】:

这是我当前服务器返回NULL 的问题。 Stack 的一位用户检查了这段代码,并告诉他得到了完美的答案。然后我在不同的服务器上检查了这个,现在它工作得很好。

【讨论】:

    猜你喜欢
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多