Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

fetch and decode coinmarketcap JSON data with PHP using file_get_contents

I want to display the current USDT Volume of upbit.

I have used coinmarketcap api for this. https://pro-api.coinmarketcap.com/v1/exchange/info

The problem i am facing is that it returns Undefined index: spot_volume_usd in.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What i am doing is

$url = "https://pro-api.coinmarketcap.com/v1/exchange/info?CMC_PRO_API_KEY={KEY}&slug=upbit"; 
$stats = json_decode(file_get_contents($url), true); 
$usdtvalue = $stats["spot_volume_usd"];


echo $usdtvalue."<br>";  

The api response is

{"status":{"timestamp":"2022-01-25T23:23:22.154Z","error_code":0,"error_message":null,"elapsed":9,"credit_count":1,"notice":null},"data":{"upbit":{"id":351,"name":"Upbit","slug":"upbit","description":null,"notice":"","logo":"https://s2.coinmarketcap.com/static/img/exchanges/64x64/351.png","countries":[],"fiats":["KRW"],"urls":{"twitter":["https://twitter.com/upbitglobal"],"chat":["https://pf.kakao.com/_DWxgVxl"],"website":["https://upbit.com/"],"fee":["https://upbit.com/service_center/guide"],"blog":[]},"tags":null,"type":"","date_launched":"2017-10-24T00:00:00.000Z","is_hidden":0,"is_redistributable":null,"maker_fee":0,"taker_fee":0,"spot_volume_usd":2684246187.212572,"spot_volume_last_updated":"2022-01-25T23:20:15.673Z","weekly_visits":3124629}}}

What is the best way to achieve this ? Thanks in advance!

>Solution :

Take a look at this schema, printed using JSONViewer tool:

enter image description here

From this, we can see that spot_volume_usd is in this path: $JSON['data']['upbit']['spot_volume_usd'].

Test it this way:

$usdtvalue = $stats['data']['upbit']['spot_volume_usd'];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading