从JSON获取数据

Just wondering if anyone knew what I was doing wrong here?

I am trying to get data from an API for bitcoin via php. However, I am getting no results from my php page.

$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"];

However I am getting nothing show at all on the php page. If I use the code below, It works and dumps out the entire information. But, I would prefer to obtain the information separately, instead of one big dump.

$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));

Anyone have any ideas why the first code block isn't working? Thanks! Very new to API and Json

Using cURL is much better

Updated code (needs error checking)

$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"];

I have printed the result it will produce following output

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
        )

)

so you can use foreach loop if your api contain multiple

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

}

full code

    <?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;

}

The setting you are looking for is allow_url_fopen.

You have two ways of getting around it without changing php.ini, one of them is to use fsockopen(), and the other is to use cURL.

I recommend using cURL over file_get_contents() anyways, since it was built for this.