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

来自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?

图片转代码服务由CSDN问答提供 功能建议

尝试使用以下JSON中的get远程内容从'price_usd'获取值: https://api.coinmarketcap.com/v1/ticker/bitcoin/

返回0值。 有人可以解释我在这里做错了吗? 谢谢! 请参阅代码:

  public function getUSD(){
 
 $ json = json_decode($ this-> getRemoteContents(“https://api.coinmarketcap.com/  v1 / ticker / bitcoin /“),true); 
 
返回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-&gt  ;超时); 
 
 curl_setopt($ ch,CURLOPT_USERAGENT,$ USER_AGENT); 
 
 curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1); 
 
 curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1); 
 \  n 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){
 
返回$ this-> getRemoteContents($ url,$ retries); 
 
} 
 
} 
 
返回$ result; 
 
} 
    
 
 

我认为它与return isset line有关?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doudou3935 2016-10-20 07:51
    已采纳

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

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题