Bitcoin Forum
May 29, 2024, 02:56:58 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Mtgox API question  (Read 715 times)
gandalfthegrey (OP)
Newbie
*
Offline Offline

Activity: 1
Merit: 0


View Profile
October 16, 2012, 03:03:53 AM
 #1

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
?>
mesquka
Member
**
Offline Offline

Activity: 70
Merit: 10


"Human equivalent of a typo."


View Profile WWW
October 16, 2012, 11:43:53 AM
 #2

This should do it:

Code:
<?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], 26);
 
   
// 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_database64_decode($secret), true)),
   );
 
   
// our curl handle (initialize if required)
   
static $ch null;
   if (
is_null($ch)) {
      
$ch curl_init();
      
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
      
curl_setopt($chCURLOPT_USERAGENT'Mozilla/4.0 (compatible; MtGox PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
   }
   
curl_setopt($chCURLOPT_URL'https://mtgox.com/api/'.$path);
   
curl_setopt($chCURLOPT_POSTFIELDS$post_data);
   
curl_setopt($chCURLOPT_HTTPHEADER$headers);
   
curl_setopt($chCURLOPT_SSL_VERIFYPEERFALSE);
 
   
// run the query
   
$res curl_exec($ch);

   
$dec json_decode($restrue);

   return 
$dec;
}

$decoded=mtgox_query('1/BTCUSD/ticker');
$lastprice = ("".$cur_avg=$decoded['return']['last']['value']."\n");
echo 
$lastprice
?>

BTC
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!