· 5 years ago · Aug 14, 2020, 06:14 AM
1private function httpRequest($url, $method = "GET", $params = array(), $signed = false) {
2 if (function_exists('curl_init') == false) {
3 throw new \Exception("Sorry cURL is not installed!");
4 }
5
6 $ch = curl_init();
7 curl_setopt($ch, CURLOPT_VERBOSE, $this->httpDebug);
8 $query = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
9
10 if ($signed == true) {
11 if (empty($this->api_key))
12 throw new \Exception("signedRequest error: API Key not set!");
13 if (empty($this->api_secret))
14 throw new \Exception("signedRequest error: API Secret not set!");
15 $base = $this->base;
16 $ts = ( microtime(true) * 1000 ) + $this->info['timeOffset'];
17 $params['timestamp'] = number_format($ts, 0, '.', '');
18 $query = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
19 $signature = hash_hmac('sha256', $query, $this->api_secret);
20 $endpoint = $base . $url . '?' . $query . '&signature=' . $signature;
21 curl_setopt($ch, CURLOPT_URL, $endpoint);
22 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
23 'X-MBX-APIKEY: ' . $this->api_key
24 ));
25 }
26 else if (count($params) > 0) {
27 curl_setopt($ch, CURLOPT_URL, $this->base . $url . '?' . $query);
28 }
29 else {
30 curl_setopt($ch, CURLOPT_URL, $this->base . $url);
31 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
32 'X-MBX-APIKEY: ' . $this->api_key
33 ));
34 }
35 curl_setopt($ch, CURLOPT_USERAGENT, "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)");
36 if ($method == "POST") {
37 curl_setopt($ch, CURLOPT_POST, true);
38 }
39 if ($method == "DELETE") {
40 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
41 }
42
43 echo "<br>endpoint=".$endpoint." query=".$query;
44 //var_dump($params);
45
46 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
47 curl_setopt($ch, CURLOPT_HEADER, false);
48 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
49 curl_setopt($ch, CURLOPT_TIMEOUT, 60);
50 $output = curl_exec($ch);
51 if (curl_errno($ch) > 0) {
52 echo 'Curl error: ' . curl_error($ch) . "\n";
53 return array();
54 }
55 curl_close($ch);
56 $json = json_decode($output, true);
57 if (isset($json['msg'])) {
58 echo "signedRequest error: {$output}" . PHP_EOL;
59 }
60 return $json;
61 }
62
63
64 public function order($symbol, $side, $type = "LIMIT", $quantity, $leverage, $takeprofit, $price, $flags = array(), $test = false) {
65 $opt = array(
66"symbol" => $symbol,
67"side" => $side,
68"type" => $type,
69"quantity" => $quantity,
70"accountId" => 12414598682060098,
71"recvWindow" => 60000,
72"timestamp" => ( microtime(true) * 1000 )
73);
74
75
76
77 if (gettype($price) != "string") {
78 $price = number_format($price, 5, '.', '');
79 }
80
81 if (is_numeric($quantity) == false) {
82 echo "warning: quantity expected numeric got " . gettype($quantity) . PHP_EOL;
83 }
84
85 if (is_string($price) == false) {
86 echo "warning: price expected string got " . gettype($price) . PHP_EOL;
87 }
88
89 if ($type === "LIMIT" || $type === "STOP_LOSS_LIMIT" || $type === "TAKE_PROFIT_LIMIT") {
90 $opt["price"] = $price;
91 $opt["timeInForce"] = "GTC";
92 }
93
94//echo "<br>type=".$type." opt_p=".$opt["price"]." opt_t=".$opt["timeInForce"];
95
96 if (isset($leverage)) {
97 $opt['leverage'] = $leverage;
98 }
99
100 if (isset($takeprofit)) {
101 $opt['takeProfit'] = $takeprofit;
102 }
103
104 if (isset($stoploss)) {
105 $opt['stopLoss'] = $stoploss;
106 }
107
108 if (isset($flags['icebergQty'])) {
109 $opt['icebergQty'] = $flags['icebergQty'];
110 }
111
112 if (isset($flags['newOrderRespType'])) {
113 $opt['newOrderRespType'] = $flags['newOrderRespType'];
114 }
115
116var_dump ($opt);
117
118 $qstring = ( $test == false ) ? "/api/v1/order" : "/api/v1/order/test";
119 return $this->httpRequest($qstring, "POST", $opt, true);
120 }