-
Notifications
You must be signed in to change notification settings - Fork 167
/
mtgox_api.php
221 lines (191 loc) · 7.2 KB
/
mtgox_api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
require_once "mtgox_config.php";
// In order to convert the int to a decimal you can...
// kind of field ...divide by ...multiply by
// BTC (volume, amount) 1E8 (10,000,000) 0.00000001
// USD (price) 1E5 (100,000) 0.00001
// JPY (price) 1E3 (1,000) 0.001
//
// Authentication is performed by signing each request using
// HMAC-SHA512. The request must contain an extra value "nonce" which
// must be an always incrementing numeric value. In addition to the
// "nonce" value, your POST data must also include your username and
// password values, named "name" and "pass" respectively. A reference
// implementation is provided here:
class MtGox_API
{
function __construct($key, $secret)
{
$this->key = $key;
$this->secret = $secret;
}
function query($path, array $req = array())
{
// 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: ' . $this->key,
'Rest-Sign: '. base64_encode(hash_hmac('sha512', $post_data, base64_decode($this->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_CAINFO, ABSPATH . "/cert/facacbc6.0");
// run the query
$res = curl_exec($ch);
if ($res === false) throw new Exception('Could not get reply: ' . curl_error($ch));
$dec = json_decode($res, true);
if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
return $dec;
}
function get_info()
{
return self::query('0/info.php');
}
////////////////////////////////////////////////////////////////////////
// * 0/getFunds.php
//
// Get your current balance
//
// https://mtgox.com/api/0/getFunds.php
////////////////////////////////////////////////////////////////////////
function get_funds()
{
return self::query('0/getFunds.php');
}
////////////////////////////////////////////////////////////////////////
// * 0/buyBTC.php
//
// Place an order to Buy BTC
//
// POST data: amount=#&price=#
//
// returns a list of your open orders
////////////////////////////////////////////////////////////////////////
function buy_btc($amount, $price)
{
return self::query('0/buyBTC.php',
array('amount' => $amount,
'price' => $price));
}
////////////////////////////////////////////////////////////////////////
// * 0/sellBTC.php
//
// Place an order to Sell BTC
//
// POST data: &amount=#&price=#
//
// returns a list of your open orders
////////////////////////////////////////////////////////////////////////
function sell_btc($amount, $price)
{
return self::query('0/sellBTC.php',
array('amount' => $amount,
'price' => $price));
}
////////////////////////////////////////////////////////////////////////
// * 0/getOrders.php
//
// Fetch a list of your open Orders
//
// returned type: 1 for sell order or 2 for buy order
//
// status: 1 for active, 2 for not enough funds
////////////////////////////////////////////////////////////////////////
function get_orders()
{
return self::query('0/getOrders.php');
}
////////////////////////////////////////////////////////////////////////
// * 0/cancelOrder.php
//
// Cancel an order
//
// POST data: oid=#&type=#
//
// oid: Order ID
////////////////////////////////////////////////////////////////////////
function cancel_order($orderid)
{
return self::query('0/cancelOrder.php',
array('oid' => $orderid));
}
////////////////////////////////////////////////////////////////////////
// * 0/redeemCode.php
//
// Used to redeem a mtgox coupon code
//
// call with a post parameter "code" containing the code to redeem
//
// it will return an array with amount (float amount value of code), currency (3 letters, BTC or USD), reference (the transaction id), and status
//
////////////////////////////////////////////////////////////////////////
function deposit_coupon($code)
{
return self::query('0/redeemCode.php',
array('code' => $code));
}
////////////////////////////////////////////////////////////////////////
// * 0/withdraw.php
//
// withdraw / Send BTC
//
// POST data: group1=BTC&btca=bitcoin_address_to_send_to&amount=#
//
// pass btca parameter to withdraw to a btc adress
// pass group1 for a coupon : BTC2CODE or USD2CODE
// pass group1=DWUSD for a dwolla withdraw
//
// return code and status if successful
////////////////////////////////////////////////////////////////////////
function withdraw_btc_coupon($amount)
{
return self::query('0/withdraw.php',
array('group1' => 'BTC2CODE',
'amount' => $amount));
}
function withdraw_fiat_coupon($amount, $currency = CURRENCY)
{
return self::query('0/withdraw.php',
array('group1' => 'USD2CODE',
'amount' => $amount,
'Currency' => $currency)); // has to have capital 'C'; $currency = 'AUD' works
}
////////////////////////////////////////////////////////////////////////
// * 0/btcAddress.php
//
// get a bitcoin deposit adress for your account
//
// returns a bitcoin deposit address
////////////////////////////////////////////////////////////////////////
function get_btc_address()
{
return self::query('0/btcAddress.php');
}
////////////////////////////////////////////////////////////////////////
// * 0/history_[CUR].csv
//
// Allows downloading your activity history for a given currency (BTC or USD for now).
//
// https://mtgox.com/api/0/history_BTC.php
// https://mtgox.com/api/0/history_USD.php
////////////////////////////////////////////////////////////////////////
function get_btc_history()
{
return self::query('0/history_BTC.csv'); /* doesn't work */
}
function get_usd_history()
{
return self::query('0/history_USD.csv'); /* doesn't work */
}
}
?>