forked from tf-redfree/PHP-btce-api
/
btce-api.php
233 lines (211 loc) · 6.96 KB
/
btce-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
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* API-call related functions
*
* @author marinu666
* @license MIT License - https://github.com/marinu666/PHP-btce-api
*/
class BTCeAPI {
const DIRECTION_BUY = 'buy';
const DIRECTION_SELL = 'sell';
protected $public_api = 'https://btc-e.com/api/2/';
protected $api_key;
protected $api_secret;
protected $noonce;
protected $RETRY_FLAG = false;
public function __construct($api_key, $api_secret, $base_noonce = false) {
$this->api_key = $api_key;
$this->api_secret = $api_secret;
if($base_noonce === false) {
// Try 1?
$this->noonce = time();
} else {
$this->noonce = $base_noonce;
}
}
/**
* Get the noonce
* @global type $sql_conx
* @return type
*/
protected function getnoonce() {
$this->noonce++;
return array(0.05, $this->noonce);
}
/**
* Call the API
* @staticvar null $ch
* @param type $method
* @param type $req
* @return type
* @throws Exception
*/
public function apiQuery($method, $req = array()) {
$req['method'] = $method;
$mt = $this->getnoonce();
$req['nonce'] = $mt[1];
// generate the POST data string
$post_data = http_build_query($req, '', '&');
// Generate the keyed hash value to post
$sign = hash_hmac("sha512", $post_data, $this->api_secret);
// Add to the headers
$headers = array(
'Sign: '.$sign,
'Key: '.$this->api_key,
);
// Create a CURL Handler for use
$ch = null;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; Marinu666 BTCE PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
curl_setopt($ch, CURLOPT_URL, 'https://btc-e.com/tapi/');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// Send API Request
$res = curl_exec($ch);
// Check for failure & Clean-up curl handler
if($res === false) {
$e = curl_error($ch);
curl_close($ch);
throw new BTCeAPIFailureException('Could not get reply: '.$e);
} else {
curl_close($ch);
}
// Decode the JSON
$result = json_decode($res, true);
// is it valid JSON?
if(!$result) {
throw new BTCeAPIInvalidJSONException('Invalid data received, please make sure connection is working and requested API exists');
}
// Recover from an incorrect noonce
if(isset($result['error']) === true) {
if(strpos($result['error'], 'nonce') > -1 && $this->RETRY_FLAG === false) {
$matches = array();
$k = preg_match('/:([0-9]+),/', $result['error'], $matches);
$this->RETRY_FLAG = true;
trigger_error("Nonce we sent ({$this->noonce}) is invalid, retrying request with server returned nonce: ({$matches[1]})!");
$this->noonce = $matches[1];
return $this->apiQuery($method, $req);
} else {
throw new BTCeAPIErrorException('API Error Message: '.$result['error'].". Response: ".print_r($result, true));
}
}
// Cool -> Return
$this->RETRY_FLAG = false;
return $result;
}
/**
* Retrieve some JSON
* @param type $URL
* @return type
*/
protected function retrieveJSON($URL) {
$opts = array('http' =>
array(
'method' => 'GET',
'timeout' => 10
)
);
$context = stream_context_create($opts);
$feed = file_get_contents($URL, false, $context);
$json = json_decode($feed, true);
return $json;
}
/**
* Place an order
* @param type $amount
* @param type $pair
* @param type $direction
* @param type $price
* @return type
*/
public function makeOrder($amount, $pair, $direction, $price) {
if($direction == self::DIRECTION_BUY || $direction == self::DIRECTION_SELL) {
$data = $this->apiQuery("Trade"
,array(
'pair' => $pair,
'type' => $direction,
'rate' => $price,
'amount' => $amount
)
);
return $data;
} else {
throw new BTCeAPIInvalidParameterException('Expected constant from '.__CLASS__.'::DIRECTION_BUY or '.__CLASS__.'::DIRECTION_SELL. Found: '.$direction);
}
}
/**
* Cancel an order
* @param type $order_id
* @return type
*/
public function cancelOrder($order_id) {
return $this->apiQuery("CancelOrder"
,array(
'order_id' => $order_id
)
);
}
/**
* Check an order that is complete (non-active)
* @param type $orderID
* @return type
* @throws Exception
*/
public function checkPastOrder($orderID) {
$data = $this->apiQuery("OrderList"
,array(
'from_id' => $orderID,
'to_id' => $orderID,
/*'count' => 15,*/
'active' => 0
));
if($data['success'] == "0") {
throw new BTCeAPIErrorException("Error: ".$data['error']);
} else {
return($data);
}
}
/**
* Public API: Retrieve the Fee for a currency pair
* @param string $pair
* @return array
*/
public function getPairFee($pair) {
return $this->retrieveJSON($this->public_api.$pair."/fee");
}
/**
* Public API: Retrieve the Ticker for a currency pair
* @param string $pair
* @return array
*/
public function getPairTicker($pair) {
return $this->retrieveJSON($this->public_api.$pair."/ticker");
}
/**
* Public API: Retrieve the Trades for a currency pair
* @param string $pair
* @return array
*/
public function getPairTrades($pair) {
return $this->retrieveJSON($this->public_api.$pair."/trades");
}
/**
* Public API: Retrieve the Depth for a currency pair
* @param string $pair
* @return array
*/
public function getPairDepth($pair) {
return $this->retrieveJSON($this->public_api.$pair."/depth");
}
}
/**
* Exceptions
*/
class BTCeAPIException extends Exception {}
class BTCeAPIFailureException extends BTCeAPIException {}
class BTCeAPIInvalidJSONException extends BTCeAPIException {}
class BTCeAPIErrorException extends BTCeAPIException {}
class BTCeAPIInvalidParameterException extends BTCeAPIException {}
?>