I have been trying to find out how to display only one value on the screen (using PHP). I have following ticker which provides values on web browser. : http://data.mtgox.com/api/1/BTCgbp/ticker I only need following value shown on screen on web page : "avg":{"value" I have no idea how to filter all other values out . Some people talking about Json what is it about? here should be some more details : https://en.bitcoin.it/wiki/MtGox/API/HTTP/v1#Multi_Currency_Ticker Thanks in advance!
Here you go: $ticker = file_get_contents("http://data.mtgox.com/api/1/BTCgbp/ticker") ; $ticker_decoded = json_decode($ticker) ; echo $ticker_decoded->return->avg->value ; PHP: Basically the ticker is returning data in JSON format. Nothing to be scraed about json, its just a javascript object (AFAIK). SO json_decode turns that js object into a php object that you can use. Simple!
Try this: http://stackoverflow.com/questions/6724467/why-doesnt-file-get-contents-work If it still doesn't work, simply use curl to download the json.
mahnazkut , try just echo'ing out file_get_contents('http://data.mtgox.com/api/1/BTCgbp/ticker'); and see what you get. Try setting error_reporting(E_ALL ^ E_NOTICE); too at the beginning of the file too. If it loads nothing, or if it just returns an error, check if your host has furl enabled. After you get it working, via file_get_contents() or cURL, try this instead kutch's approapch. <?php $ticker = json_decode(file_get_contents('http://data.mtgox.com/api/1/BTCgbp/ticker'),true); echo $ticker['return']['avg']['value']; ?> PHP: *Note the second file_get_contents parameter, "true" defines our $ticker as being an Array and not an StdObject. Let us know what you got so far with this code. If it still doesn't work, contact your host regarding furl. Demo: http://dev.bustedlocals.com/edi/ticker.php <?php $ticker = json_decode(file_get_contents('http://data.mtgox.com/api/1/BTCgbp/ticker'),true); echo '<pre>'; print_r($ticker); echo '</pre>'; echo 'Ticker avg value: '.$ticker['return']['avg']['value']; ?> PHP:
Here's my half-ass attempt, but looking at your DEMO, not sure why but my own gets a different value? <?php $ticker = file_get_contents("http://data.mtgox.com/api/1/BTCgbp/ticker"); $avg = json_decode($ticker, true); echo $avg ["return"] ["avg"] ["value"]; ?> PHP:
Well, looking into the ticker values you can see that [avg] => Array([value] => 112.25976 [...] Thats the value you should get. What are you getting though ?
On the api url i get "currency":"GBP"},"avg":{"value":"109.33497","value_int":"10933497" and the same values with the script above except yours... it reads close to what your posted 113.63863 I wonder is there any other code hidden on that page... EDIT: Never mind I think it was just cached
If i were you i would get your rates from blockchain.. they are much more accurate.. <?php $raw=file_get_contents('https://blockchain.info/ticker'); $obj = json_decode($raw, true); $currency = $obj['USD']; $rate = $obj['USD']['last']; $rate2 = $obj['USD']['24h']; $rate3 = $obj['USD']['15m']; ?> <br> <P> 1 BTC Last Rate = $<?php echo $rate;?> USD </p> <p> 15 minutes delayed market price = $<?php echo $rate3;?> USD </p> <p> Last 24 hours average price = $<?php echo $rate2;?> USD </p> Code (markup): Same deal I used json as an object