duanaoyuan7202 2016-10-20 07:44
浏览 60
已采纳

来自JSON的CURL的getRemoteContents返回0

Trying to get the value from 'price_usd' using get remote contents in PHP from the following JSON: https://api.coinmarketcap.com/v1/ticker/bitcoin/

It returns a 0 value. Can someone please explain what I am doing wrong here? Thanks! See code:

public function getUSD(){

        $json = json_decode($this->getRemoteContents("https://api.coinmarketcap.com/v1/ticker/bitcoin/"), true);

        return isset ($json['price_usd'])?($json['price_usd']):0;

    }

    public function getRemoteContents($url, $retries=5) {

        $USER_AGENT = $_SERVER['HTTP_USER_AGENT'];

        $result = "";

        if (extension_loaded('curl') === true) {

            $ch=curl_init();

            curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);

            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);

            curl_setopt($ch, CURLOPT_USERAGENT, $USER_AGENT);

            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

            curl_setopt($ch, CURLOPT_URL, $url);

            $result = curl_exec($ch);

            curl_close($ch);

            unset($ch);

        } else {

            $options  = array('http' => array('user_agent' => $USER_AGENT, 'timeout' => $this->timeout));

            $context  = stream_context_create($options);

            $result = trim(file_get_contents($url,false, $context));

        }

        if (trim($result)=="") {

            $retries-=1;

            if ($retries >= 1) {

                return $this->getRemoteContents($url,$retries);

            }

        }

    return $result;

    } 

I think it has something to do with the return isset line?

  • 写回答

1条回答 默认 最新

  • doudou3935 2016-10-20 07:51
    关注

    You need to check for $json[0]['price_usd'].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记