-
Notifications
You must be signed in to change notification settings - Fork 37
/
MtGoxClient.php
350 lines (296 loc) · 8.68 KB
/
MtGoxClient.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/**
* @package MtGox PHP API Client v2
* @author Pathaksa Tongpitak
* @version 0.1
* @access public
* @license http://www.opensource.org/licenses/LGPL-3.0
*/
class MtGoxClient
{
/**
* MtGox API Key
* @var
*/
private $apiKey;
/**
* MtGox API Secret
* @var
*/
private $apiSecret;
/**
* MtGox API Endpoint
* @var string
*/
public $endPoint;
/**
* Result Cache
* @var
*/
public $result;
/**
* Currency Pair
* @var string
*/
public $pair;
/**
* @param $apiKey
* @param $apiSecret
*/
public function __construct($apiKey, $apiSecret)
{
define('API_ERROR_EXCEPTION', 1);
$this->endPoint = 'https://data.mtgox.com/api/2/';
$this->pair = 'BTCUSD';
$this->checkRequired($apiKey,'You must specify an API Key');
$this->checkRequired($apiSecret,'You must specify an API Secret');
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
}
/**
* Queries a method of the MtGox API.
*
* @param string $method
* @param array $request
* @return mixed
* @throws Exception
*/
public function query($method, array $request = array())
{
// API settings
$apiKey = $this->apiKey;
$apiSecret = $this->apiSecret;
// generate a nonce as micro-time, with as-string handling to avoid problems with 32bits systems
$mt = explode(' ', microtime());
$request['nonce'] = $mt[1] . substr($mt[0], 2, 6);
// generate the POST data string
$postData = http_build_query($request, '', '&');
// generate Rest Payload
$restPayload = $method . "\0" . $postData;
// generate Rest Signature
$restSign = base64_encode(
hash_hmac(
'sha512',
$restPayload,
base64_decode($apiSecret),
true
)
);
// generate the extra headers
$headers = array(
'Rest-Key: ' . $apiKey,
'Rest-Sign: ' . $restSign,
'Content-type', 'application/x-www-form-urlencoded',
'Content-Length', strlen($postData)
);
// our curl handle (initialize if required)
static $ch = null;
if (is_null($ch))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT,
'Mozilla/4.0 (compatible; MtGox PHP API Client v2; ' . php_uname('s') . '; PHP/' .
phpversion() . ')');
}
// generate API url
$url = $this->endPoint . $method;
// set CURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// run the query
$response = curl_exec($ch);
if ($response === false)
{ echo "Gox error, trying again.."; sleep(2); return false; }
//throw new Exception('Unable to retrieve response: ' . curl_error($ch));
// decode JSON response
$result = json_decode($response, true);
if (!$result) { echo "Gox error, trying again.."; sleep(2); return false; } /*dann edit*/
//throw new Exception('Invalid response, make sure the API method exists');
// cache result
$this->result = $result;
return $result;
}
/**
* Get account details associated with the current API Authentication.
*
* @require API Rights: Get Info
* @return mixed
*/
function getInfo() {
$result = $this->query( $this->pair . '/money/info');
return $result;
}
/**
* Get the most recent information for a currency pair.
*
* @return mixed
*/
function getTicker() {
$result = $this->query( $this->pair . '/money/ticker');
return $result;
}
/**
* Get currency information.
*
* @return mixed
*/
function getCurrency() {
$result = $this->query( $this->pair . '/money/currency');
return $result;
}
/**
* Get information on current orders.
*
* @return mixed
*/
function getOrders() {
$result = $this->query( $this->pair . '/money/orders');
return $result;
}
/**
* Get an up-to-date quote for a bid or ask transaction.
*
* @param string $type
* @param string $amount
* @return Array
*/
function orderQuote($type = 'ask', $amount = '100000000') {
$result = $this->query( $this->pair . '/money/order/quote', array(
'type' => $type,
'amount' => $amount
));
return $result;
}
/**
* Place a bid order of a specific amount and bid price.
*
* @param float $amount
* @param $price
* @return mixed
*/
function orderBuy($amount = 0.0001, $price) {
$result = $this->orderAdd('bid', $amount = 0.0001, $price);
return $result;
}
/**
* Place an ask order of a specific amount and ask price.
*
* @param float $amount
* @param $price
* @return mixed
*/
function orderSell($amount = 0.0001, $price) {
$result = $this->orderAdd('ask', $amount = 0.0001, $price);
return $result;
}
/**
* Place an order of a specific amount and bid/ask price.
*
* @param $type
* @param float $amount
* @param $price
* @return mixed
*/
function orderAdd($type, $amount = 0.0001, $price) {
if ( !in_array($type, array('bid', 'ask')) ) {
$this->error(API_ERROR_EXCEPTION, 'You must specify a type: bid or ask');
}
$this->checkRequired($price,'You must specify a price');
$result = $this->query( $this->pair . '/money/order/add', array(
'type' => $type,
'amount_int' => $amount,
'price_int' => $price
));
return $result;
}
/**
* Cancels an order by Order ID.
*
* @param $orderId
* @return mixed
*/
function orderCancel($orderId) {
$this->checkRequired($orderId,'You must specify an Order ID');
$result = $this->query( $this->pair . '/money/order/cancel', array(
'oid' => $orderId
));
return $result;
}
/**
* Returns a unique bitcoin deposit address for a given MtGox account (new each time).
*
* @param string $account Account ID fo the following format M12345678X
* @return mixed
*/
function generateDepositAddress($account) {
$this->checkRequired($account,'You must specify an Account ID');
$result = $this->query( $this->pair . '/money/bitcoin/get_address', array(
'account' => $account
));
return $result;
}
/**
* Generates a new bitcoin address for depositing.
*
* @require API Rights: Deposit
* @param string $description Optional description to display in the account history
* @param string $ipn Optional IPN URL which will be called with details when bitcoins are received
* @return mixed
*/
function getDepositAddress($description = null, $ipn = null) {
$result = $this->query( $this->pair . '/money/bitcoin/address', array(
'description' => $description,
'ipn' => $ipn
));
return $result;
}
/**
* Gets the transaction history of a specified currency wallet.
*
* @param string $currency
* @param int $page
* @return mixed
*/
function getWalletHistory($currency = 'BTC', $page = 1) {
$result = $this->query( $this->pair . '/money/wallet/history', array(
'currency' => $currency,
'page' => $page
));
return $result;
}
/**
* Sets the current pair for calling methods which require pair prefix.
*
* @param string $pair
* @return $this
*/
function setPair($pair = 'BTCUSD') {
$this->pair = $pair;
return $this;
}
/**
* Check if a variable is not empty.
*
* @param $variable
* @param $message
*/
function checkRequired($variable, $message) {
if ($variable == '') {
$this->error(API_ERROR_EXCEPTION, $message);
}
}
/**
* Throws errors.
*
* @param $type
* @param $message
* @throws Exception
*/
function error($type, $message) {
throw new Exception($message);
}
}