【问题标题】:Retrieving data from curl and json_decode从 curl 和 json_decode 检索数据
【发布时间】:2018-01-25 13:30:31
【问题描述】:

我正在尝试从 curl 和 json_decode 结果中提取单个数据字符串,但无法正确定位数组元素:

API:

$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);

结果:

{"success":true,"message":"","result":{"Bid":0.04350003,"Ask":0.04395399,"Last":0.04350003}}

PHP:

echo 'Bid: '.$obj['result']['Bid'].'<br/>';
echo 'Ask: '.$obj['result']['Ask'].'<br/>';
echo 'Last: '.$obj['result']['Last'].'<br/>';

也试过了:

echo 'Bid: '.$obj['Bid'].'<br/>';
echo 'Ask: '.$obj['Ask'].'<br/>';
echo 'Last: '.$obj['Last'].'<br/>';

【问题讨论】:

  • 如果要将响应分配给变量,则需要将 CURLOPT_RETURNTRANSFER 设置为 true - 否则,它只会回显到屏幕
  • 太棒了!那成功了!谢谢!
  • 将来,通过在变量上使用print_rvar_dump 来调试您的代码,首先确定您正在使用的这些变量中是否有数据。整个事情本可以避免并简化为“为什么 curl 不返回数据?” ... 将 json 排除在外,因此有大量关于 json_decode 的相同答案。
  • @iainn 您应该将其发布为答案,以便 Rainer 可以接受。否则,一些随机的新手会准确地发布您所说的以获取功劳;)
  • @IncredibleHat 我做了 print_r,这就是我首先知道返回什么数据的方式:-)

标签: php arrays json decode


【解决方案1】:

你需要给你的json_decode添加一个true参数

喜欢

json_decode($execResult, true);

否则你会得到一个带有解码数据的 stdObject。

那么你应该可以使用 $obj['result']['Bid'] aso。

【讨论】:

    【解决方案2】:

    json_decode 的使用有一个可选的第二个参数 - 布尔值 - 将数据作为对象或数组返回。

    {"success":true,"message":"","result":{"Bid":0.04350003,"Ask":0.04395399,"Last":0.04350003}}
    
    $obj = json_decode( $execResult ); /* will return an object */
    echo $obj->result->Bid;
    
    $obj = json_decode( $execResult, true ); /* will return an array */
    echo $obj['result']['Bid'];
    

    刚刚运行这个作为测试..

    $data='{"success":true,"message":"","result":{"Bid":0.04350003,"Ask":0.04395399,"Last":0.04350003}}';
    
    $obj = json_decode( $data ); /* will return an object */
    echo $obj->result->Bid;
    
    $obj = json_decode( $data, true ); /* will return an array */
    echo $obj['result']['Bid'];
    

    其中显示:

    0.043500030.04350003
    

    【讨论】:

    • 嗯,输出为空...没有返回任何内容
    • 它只像上面建议的 iainn 那样工作,设置为 CURLOPT_RETURNTRANSFER,这使它工作
    【解决方案3】:

    这样使用:

    echo 'Bid: '.$obj->result->Bid.'<br/>';
    echo 'Ask: '.$obj->result->Ask.'<br/>';
    echo 'Last: '.$obj->result->Last.'<br/>';
    

    【讨论】:

      【解决方案4】:

      $json = '{"success":true,"message":"","result":{"Bid":0.04350003,"Ask":0.04395399,"Last":0.04350003}}';
      
      $obj = json_decode($json, true);// decode and convert it in array 
      
      echo 'Bid: '.$obj['result']['Bid'].'<br/>';
      echo 'Ask: '.$obj['result']['Ask'].'<br/>';
      echo 'Last: '.$obj['result']['Last'].'<br/>';
      

      【讨论】:

        【解决方案5】:

        尝试使用json_decode($string, true); 作为数组

        $ch = curl_init($uri);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
        $execResult = curl_exec($ch);
        $obj = json_decode($execResult, true);
        
        echo 'Bid: '.$obj['result']['Bid'].'<br/>';
        echo 'Ask: '.$obj['result']['Ask'].'<br/>';
        echo 'Last: '.$obj['result']['Last'].'<br/>';
        

        或使用json_decode($string); 作为对象

        $ch = curl_init($uri);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
        $execResult = curl_exec($ch);
        $obj = json_decode($execResult);
        
        echo 'Bid: '.$obj->result->Bid.'<br/>';
        echo 'Ask: '.$obj->result->Ask.'<br/>';
        echo 'Last: '.$obj->result->Last.'<br/>';
        

        【讨论】:

          猜你喜欢
          • 2015-10-24
          • 1970-01-01
          • 1970-01-01
          • 2014-02-24
          • 2014-02-10
          • 1970-01-01
          • 2015-10-05
          • 1970-01-01
          • 2015-12-10
          相关资源
          最近更新 更多