1

I want to fetch the current ETH-USD price from this api (https://api.coinmarketcap.com/v1/ticker/ethereum/?convert=USD). How do I fetch just the price into a variable in PHP for reuse on my application?

enter image description here

1

You're going to have to access the url, read the contents of the url, and then turn the contents into an object or array (in this case it'll be an array).

<?php

  $url = 'https://api.coinmarketcap.com/v1/ticker/ethereum/?convert=USD';
  $data = file_get_contents($url);
  $priceInfo = json_decode($data);

  echo $priceInfo[0]->price_usd;

?>

The price_usd can be changed depending on what data you want to grab. For example, this code would give you $463.531.

|improve this answer|||||
0

Coinmarketcap is starting to charge for API. Perhaps you could have a look at CoinGecko API instead: https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=ethereum

Here is the api docs: https://www.coingecko.com/api/docs/v3

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.