Bitcoin Forum
December 12, 2017, 12:56:46 AM *
News: Latest stable version of Bitcoin Core: 0.15.1  [Torrent].
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: 0.1+ BTC for mtgox-api php code snipped (recent buys/sells)  (Read 720 times)
pangu
Sr. Member
****
Offline Offline

Activity: 351



View Profile
April 21, 2013, 10:15:30 AM
 #1

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

1513040206
Hero Member
*
Offline Offline

Posts: 1513040206

View Profile Personal Message (Offline)

Ignore
1513040206
Reply with quote  #2

1513040206
Report to moderator
1513040206
Hero Member
*
Offline Offline

Posts: 1513040206

View Profile Personal Message (Offline)

Ignore
1513040206
Reply with quote  #2

1513040206
Report to moderator
1513040206
Hero Member
*
Offline Offline

Posts: 1513040206

View Profile Personal Message (Offline)

Ignore
1513040206
Reply with quote  #2

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

Posts: 1513040206

View Profile Personal Message (Offline)

Ignore
1513040206
Reply with quote  #2

1513040206
Report to moderator
1513040206
Hero Member
*
Offline Offline

Posts: 1513040206

View Profile Personal Message (Offline)

Ignore
1513040206
Reply with quote  #2

1513040206
Report to moderator
1513040206
Hero Member
*
Offline Offline

Posts: 1513040206

View Profile Personal Message (Offline)

Ignore
1513040206
Reply with quote  #2

1513040206
Report to moderator
daybyter
Legendary
*
Offline Offline

Activity: 965


View Profile
May 10, 2013, 07:35:58 PM
 #2

Marketplace => Services

NLNico
Legendary
*
hacker
Offline Offline

Activity: 1540


DiceSites.com owner


View Profile WWW
May 11, 2013, 01:36:56 AM
 #3

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 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

NLNico
Legendary
*
hacker
Offline Offline

Activity: 1540


DiceSites.com owner


View Profile WWW
May 17, 2013, 01:38:29 AM
 #4

Not good?  Lips sealed Cheesy

miaviator
Donator
Hero Member
*
Offline Offline

Activity: 672


It's for the children!


View Profile WWW
May 21, 2013, 02:35:58 PM
 #5

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);
}

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!