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