Bitcoin Forum
October 20, 2017, 10:05:43 PM *
News: Latest stable version of Bitcoin Core: 0.15.0.1  [Torrent]. (New!)
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Mtgox API question  (Read 634 times)
gandalfthegrey
Newbie
*
Offline Offline

Activity: 1


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

Posts: 1508537143

View Profile Personal Message (Offline)

Ignore
1508537143
Reply with quote  #2

1508537143
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1508537143
Hero Member
*
Offline Offline

Posts: 1508537143

View Profile Personal Message (Offline)

Ignore
1508537143
Reply with quote  #2

1508537143
Report to moderator
gweedo
Legendary
*
Offline Offline

Activity: 1246


Java, PHP, HTML/CSS Programmer for Hire!


View Profile WWW
October 16, 2012, 04:48:18 AM
 #2

Code:
$decoded=mtgox_query('1/BTCUSD/ticker');

then you need
Code:
$lastprice = ("".$cur_avg=$decoded['return']['last']['value']."\n");


Want to earn 2500 SATOSHIS per hour? Come Chat and Chill in https://goseemybits.com/lobby
mesquka
Member
**
Offline Offline

Activity: 77



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

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:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!