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