· 6 years ago · Feb 03, 2019, 01:10 AM
1function btce_query($method, array $req = array()) {
2 // API settings
3 $key = ''; // your API-key
4 $secret = ''; // your Secret-key
5
6 $req['method'] = $method;
7 $mt = explode(' ', microtime());
8 $req['nonce'] = $mt[1];
9
10 // generate the POST data string
11 $post_data = http_build_query($req, '', '&');
12
13 $sign = hash_hmac("sha512", $post_data, $secret);
14
15 // generate the extra headers
16 $headers = array(
17 'Sign: '.$sign,
18 'Key: '.$key,
19 );
20
21 // our curl handle (initialize if required)
22 static $ch = null;
23 if (is_null($ch)) {
24 $ch = curl_init();
25 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
26 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; BTCE PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
27 }
28 curl_setopt($ch, CURLOPT_URL, 'https://btc-e.ru/tapi/');
29 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
30 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
31 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
32
33 // run the query
34 $res = curl_exec($ch);
35 if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
36 $dec = json_decode($res, true);
37 if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
38 return $dec;
39}
40
41$result = btce_query("getInfo");
42//$result = btce_query("Trade", array("pair" => "btc_usd", "type" => "buy", "amount" => 1, "rate" => 10)); //buy 1 BTC @ 10 USD
43
44echo "<pre>".print_r($result, true)."</pre>";