I am trying to get some elements through the CoinMarketCAP API for BitCoin. I would like to print the price feature from the following JSON string:
stdClass Object ( [status] => stdClass Object ( [timestamp] => 2020-03-14T12:11:35.816Z [error_code]
=> 0 [error_message] => [elapsed] => 13 [credit_count] => 1 [notice] => ) [data] => stdClass Object (
[1] => stdClass Object ( [id] => 1 [name] => Bitcoin [symbol] => BTC [slug] => bitcoin
[num_market_pairs] => 7807 [date_added] => 2013-04-28T00:00:00.000Z [tags] => Array ( [0] => mineable
) [max_supply] => 21000000 [circulating_supply] => 18270112 [total_supply] => 18270112 [platform] =>
[cmc_rank] => 1 [last_updated] => 2020-03-14T12:10:40.000Z [quote] => stdClass Object ( [USD] =>
stdClass Object ( [price] => 5399.34939057 [volume_24h] => 49810702512.923 [percent_change_1h] =>
-0.117356 [percent_change_24h] => -3.12792 [percent_change_7d] => -40.7189 [market_cap] =>
98646718092.846 [last_updated] => 2020-03-14T12:10:40.000Z ) ) ) ) )
I am using the following code, but it's not working:
<?php
$url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest';
$parameters = [
'id' => '1'
];
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: xxx'
];
$qs = http_build_query($parameters); // query string encode the parameters
$request = "{$url}?{$qs}"; // create the request URL
$curl = curl_init(); // Get cURL resource
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = json_decode(curl_exec($curl));// print json decoded response
print_r($response['data']['id']);
curl_close($curl); // Close request
?>