2017-09-14 152 views
0

想知道是否有人知道我在这里做错了什么?从JSON获取数据

我想通过PHP从比特币的API获取数据。但是,我没有从我的PHP页面获得结果。

$url = "https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=EUR"; 
    $json = file_get_contents($url); 
    $json_data = json_decode($json, true); 
    echo "ID: ". $json_data["id"]; 

但是我在php页面上什么都没有显示。如果我使用下面的代码,它工作并丢弃整个信息。但是,我宁愿单独获取信息,而不是一个大的转储。

$url = "https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=EUR"; 

$ch = curl_init(); 
// Disable SSL verification 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
// Will return the response, if false it print the response 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
// Set the url 
curl_setopt($ch, CURLOPT_URL,$url); 
// Execute 
$result=curl_exec($ch); 
// Closing 
curl_close($ch); 

var_dump(json_decode($result, true)); 

任何人有任何想法,为什么第一个代码块不工作?谢谢!很新的API和JSON

+0

你可以在这里发布var转储吗? – Andreas

+0

array(1){[0] => array(17){[“id”] => string(7)“bitcoin”[“name”] => string(7)“Bitcoin”[“symbol”] = > string(3)“BTC”[“rank”] => string(1)“1”[“price_usd”] => string(7)“3827.53”[“price_btc”] => string(3) [“24h_volume_usd”] => string(12)“2068260000.0”[“market_cap_usd”] => string(13)“63400783862.0”[“available_supply”] => string(10)“16564412.0”[“total_supply”] => string (10)“16564412.0”[“percent_change_1h”] => string(5)“-1.23”[“percent_change_24h”] => string(5)“-4.26”[“percent_change_7d”] => string(6)“-15.62 “[”last_updated“] => string(10)”1505360670“[”price_eur“] => – timbats1993

+0

您是否尝试过使用'echo”ID:“。 $ json_data [0] [ “身份证”];'? – ravisachaniya

回答

1

使用cURL好得多

更新后的代码(需要错误检查)

$url = "https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=EUR"; 

$ch = curl_init(); 
// Disable SSL verification 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
// Will return the response, if false it print the response 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
// Set the url 
curl_setopt($ch, CURLOPT_URL,$url); 
// Execute 
$result=curl_exec($ch); 
// Closing 
curl_close($ch); 

$json_data = json_decode($result, true); 

foreach ($json_data as $item) 
    echo "ID: ". $item["id"]; 
+0

似乎还没有返回。任何想法为什么? – timbats1993

+0

尝试更新代码 - 它似乎返回嵌套数组 – GeorgeQ

+0

完美!非常感谢你 – timbats1993

0

我已经印刷会产生以下输出

echo "<pre>"; 
print_r(json_decode($result, true)); 


Array 
(
    [0] => Array 
     (
      [id] => bitcoin 
      [name] => Bitcoin 
      [symbol] => BTC 
      [rank] => 1 
      [price_usd] => 3821.37 
      [price_btc] => 1.0 
      [24h_volume_usd] => 2089880000.0 
      [market_cap_usd] => 63298556016.0 
      [available_supply] => 16564362.0 
      [total_supply] => 16564362.0 
      [percent_change_1h] => -1.72 
      [percent_change_24h] => -4.57 
      [percent_change_7d] => -15.76 
      [last_updated] => 1505359771 
      [price_eur] => 3214.536444 
      [24h_volume_eur] => 1758007056.0 
      [market_cap_eur] => 53246745321.0 
     ) 

) 

所以你可以使用foreach循环,如果您的API包含多个

$data=json_decode($result, true); 
foreach($data as $key=>$val){ 
echo $val->id; 

} 

全码

结果
<?php 
    $url = "https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=EUR"; 

    $ch = curl_init(); 
    // Disable SSL verification 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    // Will return the response, if false it print the response 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    // Set the url 
    curl_setopt($ch, CURLOPT_URL,$url); 
    // Execute 
    $result=curl_exec($ch); 
    // Closing 
    curl_close($ch); 
    $data=json_decode($result, true)); 

foreach($data as $key=>$val){ 
echo $val->id; 

} 
+0

似乎没有回应/空白页面。任何想法为什么?谢谢! – timbats1993

+0

希望你能够使用var_dump打印数据,它会显示data.i认为你可以使用我的代码一次并测试它 – iCoders

0

设置你所追求的是allow_url_fopen选项。

你有两种方法可以避免改变php.ini,其中一个是使用fsockopen(),另一个是使用cURL。

无论如何,我推荐使用cURL over file_get_contents(),因为它是为此而构建的。