Author

Topic: 0.1+ BTC for mtgox-api php code snipped (recent buys/sells) (Read 824 times)

donator
Activity: 686
Merit: 519
It's for the children!
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);
}
legendary
Activity: 1876
Merit: 1295
DiceSites.com owner
legendary
Activity: 1876
Merit: 1295
DiceSites.com owner
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:
$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 '';
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 '';
}
}
echo '
DateTypeBTC'$currency .'Price
'date('d-m-y H:i',$cur_trade['Date']) .''. ($cur_trade['Type']=='earned'?'sell':'buy') .''$cur_trade['Trade']['Amount']['value'] .' BTC'$cur_trade['Value']['display'] .''$price .'
'
;
}
}
?>

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 Smiley

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 Smiley
legendary
Activity: 965
Merit: 1000
Marketplace => Services
hero member
Activity: 2912
Merit: 604
CoinMetro
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
Jump to: