· 5 years ago · Jul 08, 2020, 08:40 AM
1<?php
2
3class Api {
4
5 protected $options = [];
6
7 public function __construct($options = [])
8 {
9 $default_options = array(
10 'api_host' => 'host',
11 'public_key' => 'public key',
12 'private_key' => 'private key',
13 );
14 $this->options = array_merge($default_options, $options);
15 }
16
17
18 public function set_option($key, $value)
19 {
20 $this->options[$key] = $value;
21 }
22
23
24 public function get($path, $params = [])
25 {
26 return $this->request($path, $params);
27 }
28
29
30 public function post($path, $params = [])
31 {
32 return $this->request($path, $params, 'POST');
33 }
34
35
36 public function request($path, $params = [], $canonical_verb='GET')
37 {
38 $headers = [];
39
40 $tonce = time() * 1000;
41 $api_host = $this->options['api_host'];
42
43 ksort($params);
44
45 $is_post = ($canonical_verb == 'POST');
46 if (!$is_post) {
47 $canonical_verb = 'GET';
48 } else {
49 $headers[] = 'Content-Type: application/json';
50 $headers[] = 'x-api-key: ' . $this->options['public_key'];
51 $headers[] = 'x-api-tonce: ' . $tonce;
52 $headers[] = 'x-api-signature: ' . hash_hmac('sha512', $path . $tonce . json_encode($params, JSON_UNESCAPED_UNICODE), $this->options['private_key']);
53 }
54
55 $canonical_uri = strtolower($path);
56 if (substr($api_host , -1) == '/') {
57 $api_host = substr($api_host , 0, -1);
58 }
59
60 $url = $api_host . $path;
61 $query_str = http_build_query($params);
62 if ($is_post) {
63 $content = $this->contents($url, json_encode($params, JSON_UNESCAPED_UNICODE), $headers);
64 } else {
65 if (!empty($params)) {
66 $url .= '?' . $query_str;
67 }
68 $content = $this->contents($url, null, $headers);
69 }
70
71 if (empty($content)) {
72 throw new Exception('Content is empty');
73 }
74
75 $obj = json_decode($content, true);
76 if (empty($obj)) {
77 throw new Exception('JSON decode failed, content: ' . $content);
78 }
79 return $obj;
80 }
81
82
83 public function contents($url, $post_params = null, $headers = [])
84 {
85 $ch = curl_init();
86
87 curl_setopt($ch, CURLOPT_AUTOREFERER, true);
88 curl_setopt($ch, CURLOPT_HEADER, false);
89 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
90 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
91 curl_setopt($ch, CURLOPT_TIMEOUT, 60000);
92 curl_setopt($ch, CURLOPT_USERAGENT, 'API Client');
93
94 curl_setopt($ch, CURLOPT_URL, $url);
95 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
96 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
97 if (!empty($post_params)) {
98 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
99 curl_setopt($ch, CURLOPT_POST, true);
100 if (is_array($post_params)) {
101 $post_params = http_build_query($post_params);
102 }
103 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
104 }
105 $data = curl_exec($ch);
106 $curl_errno = curl_errno($ch);
107 $curl_error = curl_error($ch);
108 curl_close($ch);
109 if ($curl_errno > 0) {
110 throw new Exception("cURL Error ($curl_errno): $curl_error, url: {$url}");
111 }
112 return $data;
113 }
114}
115
116$client = new Api([
117 'api_host' => 'https://creekex.com',
118 'public_key' => 'your public key',
119 'private_key' => 'your private key'
120]);
121
122
123try {
124 print_r($client->get('/api/market'));
125 //print_r($client->post('/api/v1/trading/order/create', ['market' => 'btc_usd', 'type' => 'sell', 'price' => 1, 'amount' => 1]));
126} catch (Exception $e) {
127 print_r($e->getMessage());
128}