-2

I'm trying to make a simple website that uses the coinmarketcap api to track current cryptocurrency prices, and the code below works but it doesn't account for when currencies overtake each other.

    <?php
    $tick = file_get_contents('https://api.coinmarketcap.com/v1/ticker/');
    $url = $tick;
    echo $url;
    $json = file_get_contents($url);
    $data = json_decode($tick, TRUE); //decodes in associative array

    $ethusd = $data[1]['price_usd'];
    echo $ethusd;

    ?>

And this is the json being decoded

[
{
    "id": "bitcoin", 
    "name": "Bitcoin", 
    "symbol": "BTC", 
    "rank": "1", 
    "price_usd": "16474.4", 
    "price_btc": "1.0", 
    "24h_volume_usd": "13440600000.0", 
    "market_cap_usd": "275760451140", 
    "available_supply": "16738725.0", 
    "total_supply": "16738725.0", 
    "max_supply": "21000000.0", 
    "percent_change_1h": "-2.32", 
    "percent_change_24h": "-5.68", 
    "percent_change_7d": "24.41", 
    "last_updated": "1513187054"
}, 
{
    "id": "ethereum", 
    "name": "Ethereum", 
    "symbol": "ETH", 
    "rank": "2", 
    "price_usd": "684.996", 
    "price_btc": "0.04138", 
    "24h_volume_usd": "4731760000.0", 
    "market_cap_usd": "65976663404.0", 
    "available_supply": "96316859.0", 
    "total_supply": "96316859.0", 
    "max_supply": null, 
    "percent_change_1h": "-5.62", 
    "percent_change_24h": "12.04", 
    "percent_change_7d": "56.0", 
    "last_updated": "1513187055"
}, 
{
    "id": "bitcoin-cash", 
    "name": "Bitcoin Cash", 
    "symbol": "BCH", 
    "rank": "3", 
    "price_usd": "1594.38", 
    "price_btc": "0.0963151", 
    "24h_volume_usd": "1286400000.0", 
    "market_cap_usd": "26871042768.0", 
    "available_supply": "16853600.0", 
    "total_supply": "16853600.0", 
    "max_supply": "21000000.0", 
    "percent_change_1h": "-3.9", 
    "percent_change_24h": "1.8", 
    "percent_change_7d": "8.88", 
    "last_updated": "1513187076"
}, 
etc.

Since ether is the second highest currency in the json it works for now, but if it moves it wouldn't. So is there a way for me to do something like $data[something that specifies ether]['price_usd'] instead? Apologies in advance for any PHP ignorance, I know very little about PHP/arrays/etc and just started working with it a few days ago.

1

2 Answers 2

0

I played a little bit with their API and you can fetch only 1 currency by appending the slug of the currency in the url:

example https://api.coinmarketcap.com/v1/ticker/ethereum/

so your endpoint becomes https://api.coinmarketcap.com/v1/ticker/{slug}/

/!\ Be aware that this endpoint still sends you a JSON array so you have to take the first index with $data[0]. Your code becomes :

<?php
$slug = 'ethereum'; // find all at https://files.coinmarketcap.com/generated/search/quick_search.json
$tick = file_get_contents('https://api.coinmarketcap.com/v1/ticker/'.$slug.'/');
$url = $tick;
echo $url;
$json = file_get_contents($url);
$data = json_decode($tick, TRUE); //decodes in associative array

$ethusd = $data[0]['price_usd'];
echo $ethusd;

?>

All slugs are listed here https://files.coinmarketcap.com/generated/search/quick_search.json

PS: API docs are here https://coinmarketcap.com/api/ but it doesn't mention the slug endpoint

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that was very helpful but I probably should have mentioned that Ethereum wasn't the only coin I was trying to fetch my bad. I got it working using YouneL's solution and just repeated the $key_x part for each coin I needed.
0

What I understand here is that you want to get the price_usd only for ethereum id, to do so you can use array_columns to extract all ids as one array and then search the index of 'ethereum':

$key_ethereum = array_search('ethereum', array_column($data, 'id'));

// array_column($data, 'id') => ['bitcoin', 'ethereum', 'bitcoin-cash']
// array_search('ethereum', ['bitcoin', 'ethereum', 'bitcoin-cash']); => 1

echo $data[ $key_ethereum ]['price_usd'];

Live Demo

Hope this helps

2 Comments

Worked perfectly thank you! Ethereum wasn't the only coin I was trying to get price_usd for but I just repeated the $key_x for each coin I needed and it worked.
You are welcome, you may want to accept my answer if it works for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.