1

I'm trying to get the last price in USD from this coin:

<?php

$json = file_get_contents('https://api.coinmarketcap.com/v2/ticker/2424/');
echo $json;

?>

I tried two different options on the JSON object like [0]['price]
Nothing works.

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
joe
  • 11
  • 3
  • First you need to use [`json_decode()`](http://php.net/manual/en/function.json-decode.php) – aynber Jul 09 '18 at 20:11
  • Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – user3942918 Jul 10 '18 at 02:31

1 Answers1

1

The response from coinmarketcap is in JSON, so you need to json_decode it.

The second paramter of json_decode lets you decide if its a object or array. For an array, mark this as true

<?php

$json = file_get_contents('https://api.coinmarketcap.com/v2/ticker/2424/');
$objResponse = json_decode($json, true);

echo $objResponse['data']['quotes']['USD']['price'];
Oli Girling
  • 605
  • 8
  • 14