Bitcoin Forum

Economy => Services => Topic started by: pangu on April 21, 2013, 10:15:30 AM



Title: 0.1+ BTC for mtgox-api php code snipped (recent buys/sells)
Post by: pangu on April 21, 2013, 10:15:30 AM
will give you 0.1+ BTC for a php-code script, which read out all my recent buys/sells with the mt.gox-api (EUR<>BTC).
-> https://mtgox.com/trade/account-history?currency=BTC
-> https://mtgox.com/trade/account-history?currency=EUR

should give out something like:
# | bid/ask | BTC | EUR


Title: Re: 0.1+ BTC for mtgox-api php code snipped (recent buys/sells)
Post by: daybyter on May 10, 2013, 07:35:58 PM
Marketplace => Services


Title: Re: 0.1+ BTC for mtgox-api php code snipped (recent buys/sells)
Post by: NLNico on May 11, 2013, 01:36:56 AM
Just made something with help of the API documentation. Make sure to create Advanced API Key at https://mtgox.com/security (only need Get info rights) and add it in the script (line 6/7.)

Code:
<?php
$currency 
'EUR';

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'''&');
 
$prefix '';
if (substr($path02) == '2/') {
$prefix substr($path2)."\0";
}
 
// generate the extra headers
$headers = array(
'Rest-Key: '.$key,
'Rest-Sign: '.base64_encode(hash_hmac('sha512'$prefix.$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://data.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);
if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
$dec json_decode($restrue);
if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
return $dec;
}
 
// Get order history
$cur_trades mtgox_query('2/money/wallet/history',array('currency'=>$currency));

// Check if not empty
if ($cur_trades['result'] == 'success') {
if (!empty($cur_trades['data']['records']) && $cur_trades['data']['records'] > 0) {

// Start table and loop
echo '<table><tr><th>Date</th><th>Type</th><th>BTC</th><th>'$currency .'</th><th>Price</th></tr>';
foreach($cur_trades['data']['result'] as $cur_trade) {

if (in_array($cur_trade['Type'],array('earned','spent'))) {
// Calculate price
$price $cur_trade['Value']['value_int']/$cur_trade['Trade']['Amount']['value_int']*1000;

// Show trade
echo '<tr><td>'date('d-m-y H:i',$cur_trade['Date']) .'</td><td>'. ($cur_trade['Type']=='earned'?'sell':'buy') .'</td><td>'$cur_trade['Trade']['Amount']['value'] .' BTC</td><td>'$cur_trade['Value']['display'] .'</td><td>'$price .'</td></tr>';
}
}
echo '</table>';
}
}
?>

Just thought I will put it in public so everyone can use it. It uses API V2 so it will last for long. Donation on the address of my signature is very welcome though :)

Obviously it doesn't have CSS and it looks pretty bad. But I assume you can change that. If something is not working correctly, or you want some extra info from the trades, please tell me :)


Title: Re: 0.1+ BTC for mtgox-api php code snipped (recent buys/sells)
Post by: NLNico on May 17, 2013, 01:38:29 AM
Not good?  :-X :D


Title: Re: 0.1+ BTC for mtgox-api php code snipped (recent buys/sells)
Post by: miaviator on May 21, 2013, 02:35:58 PM
Well It finally got API V2 working for me.  None of the documentation sites include PHP -> they just say you have to include a null rest sign and Blah Blah Blah. 

I removed some unnecessary "stuff" from your function, as in unnecessary for my purpose:

Code:
function mtgox_query($path, array $req = array()) {
$key = '9c85694a-6b88-427b-a8e4-62d5366aa6a9'; //Your API Key
$secret = 'GD9heuq1k0MO49dKCFJawQLNMY2izza+wTjAe/GkKf+2KnaZC5Zf/qUsiQSWNIN5ltwtYahygZb7i2UI2I48TQ=='; //Your API Secret
$req['nonce'] = $GLOBALS['nonce']++; //Incriment the Global Nonce (Allows for multiple polls per second)
$post_data = http_build_query($req, '', '&'); // generate the POST data string

$prefix = $path."\0";

// generate the extra headers
$headers = array(
'Rest-Key: '.$key,
'Rest-Sign: '.base64_encode(hash_hmac('sha512', $prefix.$post_data, base64_decode($secret), true)),
);

$ch = null;
$ch = curl_init();
//curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; GOXBOT; '.php_uname('s').'; PHP/'.phpversion().')');
curl_setopt($ch, CURLOPT_URL, 'https://data.mtgox.com/api/2/'.$path);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

writeInc($GLOBALS['nonce']);

return curl_exec($ch);
}