Title: Mtgox API question Post by: gandalfthegrey 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 ?> Title: Re: Mtgox API question Post by: mesquka on October 16, 2012, 11:43:53 AM This should do it:
Code: <?php function mtgox_query($path, array $req = array()) { BTC |