Tony Starke
Tony Starke

Reputation: 23

Need to grab double nested Bitcoin price data from json url

Greetings members of the Programming Technorati.

I am new to json code and I need to capture just the 7day price of bitcoin(USD-7d:$13k) from a url json feed. I was hoping to use an include file to insert the variable into a calculation script. I tried the answers in similar questions to no avail. Kept getting

"PHP Warning: file_get_contents(api.coindesk.com/v1/bpi/currentprice.json): failed to open stream: No such file or directory in C:\Users\tony\AppData\Local\Temp~Untitled 36 on line 9 PHP Stack trace: PHP 1. {main}() C:\Users\tony\AppData\Local\Temp~Untitled 36:0 PHP 2. file_get_contents() C:\Users\tony\AppData\Local\Temp~Untitled 36:9 PHP Notice: Trying to get property of non-object in C:\Users\tony\AppData\Local\Temp~Untitled 36 on line 11 PHP Stack trace: PHP 1. {main}() C:\Users\tony\AppData\Local\Temp~Untitled 36:0"

Here is the code from the feed and the link:

$url = "http://api.bitcoincharts.com/v1/weighted_prices.json";

{"USD": {"7d": "13296.34", "30d": "12025.73", "24h": "13408.16"}, "AUD": {"7d": "18774.69", "30d": "17142.39", "24h": "19058.67"}, "RUB": {"7d": "1035007.80", "30d": "922673.28", "24h": "1070590.15"}, "timestamp": 1604082063, "NGN": {"7d": "6133867.43", "30d": "5505965.40", "24h": "6263304.03"}, "MYR": {"7d": "54960.02", "30d": "50182.16", "24h": "55868.13"}, "IDR": {"7d": "193825061.23", "30d": "175602568.25", "24h": "196535067.10"}, "KRW": {"7d": "14969248.94", "30d": "13872156.60", "24h": "15154007.63"}, "JPY": {"7d": "1386958.38", "30d": "1265196.85", "24h": "1398954.62"}, "VND": {"7d": "305351069.29", "30d": "273830432.27", "24h": "313490908.60"}, "CAD": {"7d": "17474.05", "30d": "15875.06", "24h": "17909.30"}, "ILS": {"7d": "46246.34", "30d": "41991.34", "24h": "47925.89"}, "GBP": {"7d": "10114.22", "30d": "9046.31", "24h": "10249.05"}, "PLN": {"7d": "51762.57", "30d": "46309.08", "24h": "53337.62"}, "BRL": {"7d": "75883.54", "30d": "68064.45", "24h": "77871.64"}, "ZAR": {"7d": "223603.49", "30d": "204300.99", "24h": "225720.34"}, "EUR": {"7d": "11270.77", "30d": "10127.30", "24h": "11474.41"}}

Upvotes: 1

Views: 116

Answers (1)

berend
berend

Reputation: 631

This might help

use json_decode(..,true) to turn the result into an associative array. This makes it relatively easy to reference the data you need.

<?php
$url = "http://api.bitcoincharts.com/v1/weighted_prices.json";
$arr = json_decode(file_get_contents($url), true);

echo $arr['USD']['7d'];

Output:

13305.62

Upvotes: 2

Related Questions