Author

Topic: Simplest way of obtaining MtGox exchange rate via an API? (Read 1730 times)

member
Activity: 99
Merit: 10
There might be a better way to do it, but i've tested it and this works:

Two pages, change the refresh interval in index.php

Page 1 = index.php -



      Bitcoin Price
      
      
      
      

      
      
      



Wait 10 Seconds..






Page 2 = gox.php -

header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.

$data = file_get_contents("https://data.mtgox.com/api/2/BTCUSD/money/ticker_fast");
$data = json_decode($data, true);
$spot_last = $data['data']['last']['value'];
print_r($spot_last);
?>

Try That.
member
Activity: 99
Merit: 10
Is it being cached? I haven't tested it but try this:

header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.

$data = get_content("https://data.mtgox.com/api/2/BTCUSD/money/ticker");

// If that doesn't work (old) try: file_get_contents
// $data = file_get_contents("https://data.mtgox.com/api/2/BTCUSD/money/ticker");

$data = json_decode($data, true);

$spot_last = $data['data']['last']['value'];

print_r($spot_last);
?>
legendary
Activity: 1876
Merit: 1295
DiceSites.com owner
See here the unofficial documentation about ticker_fast: https://bitbucket.org/nitrous/mtgox-api/overview#markdown-header-moneyticker_fast

Should be data.last.value and there is only a 1 second cache. Make sure it's not the browser that cached it or something? Because for me it is changing just fine (compared to bitcoinity, of course if there is no trade for a minute.. :p)
newbie
Activity: 44
Merit: 0
If it's data.last.value, why doesn't it change often? I've been sitting here refeshing the page for 2 minutes and that value is still at 111.04358.
member
Activity: 99
Merit: 10
But you're extracting buy/sell, not the exchange rate of the last trade.

I think the last sell is just "last", so:

$spot_last = $data['data']['last']['value'];
legendary
Activity: 965
Merit: 1000
I wrote some Javascript for btc-e, that could be converted to mtgox with minimal modifications:

https://bitcointalksearch.org/topic/collab-for-phonegap-app-for-btc-e-135392

It fetches the depth in an infinite loop and displays the depth.
newbie
Activity: 44
Merit: 0
But you're extracting buy/sell, not the exchange rate of the last trade.
member
Activity: 99
Merit: 10
I'm not sure about "ticker_fast" but I use this and refresh with jQuery:

$data = get_content("https://data.mtgox.com/api/2/BTCUSD/money/ticker");

// If that doesn't work (old) try: file_get_contents
// $data = file_get_contents("https://data.mtgox.com/api/2/BTCUSD/money/ticker");

$data = json_decode($data, true);

$spot_sell = $data['data']['sell']['value'];
$spot_buy = $data['data']['buy']['value'];

print_r($spot_sell);
// or
// print_r($spot_buy);
?>
newbie
Activity: 44
Merit: 0
Hey guys,

I want to do a simple app that refreshes every minute to tell me the current exchange rate by Mtgox but I have a problem interpreting the JSON that some APIs return:

 - Mtgox's fast ticker returns [this JSON][1]. The question is, what's the value that I have to retrieve? There are a lot and I don't understand them, and I don't find proper docs. (Some people say it's simply the data.last.value element of that JSON, but that doesn't seem to change overtime! unless it returns cached responses of more than 1 minute Sad wasn't the "fast ticker" supposed to be super fast?)

 - Bitcoincharts' API has the [Markets Data endpoint][2], and it returns the market I want under the symbol "mtgoxUSD", however it tells me the date time of latest trade, the last buy/ask, but doesn't tell me **the exchange rate of the latest trade**.

Am I missing something?
Thanks

  [1]: http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast
  [2]: http://bitcoincharts.com/about/markets-api/
Jump to: