Bitcoin Forum
July 03, 2024, 05:00:13 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
  Home Help Search Login Register More  
  Show Posts
Pages: [1]
1  Other / Beginners & Help / Mtgox API question on: October 16, 2012, 03:03:53 AM
I am trying to use the Mtgox v1 API to display the last Mtgox price, using php. I have successfully written/found some code which does this using version 0 API, which i have extracted below. Could somebody help me update this code to work with version 1? Will tip, thanks.

<?php function mtgox_query($path, array $req = array()) {
   // API settings
   $key = '';
   $secret = '';

   // generate a nonce as microtime, with as-string handling to avoid problems with 32bits systems
   $mt = explode(' ', microtime());
   $req['nonce'] = $mt[1].substr($mt[0], 2, 6);
 
   // generate the POST data string
   $post_data = http_build_query($req, '', '&');
 
   // generate the extra headers
   $headers = array(
      'Rest-Key: '.$key,
      'Rest-Sign: '.base64_encode(hash_hmac('sha512', $post_data, base64_decode($secret), true)),
   );
 
   // our curl handle (initialize if required)
   static $ch = null;
   if (is_null($ch)) {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MtGox PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
   }
   curl_setopt($ch, CURLOPT_URL, 'https://mtgox.com/api/'.$path);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 
   // run the query
   $res = curl_exec($ch);

   $dec = json_decode($res, true);

   return $dec;
}

$decoded=mtgox_query('0/data/ticker.php');
$lastprice = ("".$cur_avg=$decoded['ticker']['last']."\n");
echo $lastprice
?>
Pages: [1]
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!