· 6 years ago · Nov 14, 2019, 07:04 PM
1<?php
2
3class CurlPost {
4
5 private $url;
6 private $options;
7
8 public function __construct($url, array $options = [])
9 {
10 $this->url = $url;
11 $this->options = $options;
12 }
13
14 public function __invoke(array $post)
15 {
16 $ch = curl_init($this->url);
17
18 foreach ($this->options as $key => $val) {
19 curl_setopt($ch, $key, $val);
20 }
21
22 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
23 curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
24
25 $response = curl_exec($ch);
26 $error = curl_error($ch);
27 $errno = curl_errno($ch);
28
29 if (is_resource($ch)) {
30 curl_close($ch);
31 }
32
33 if (0 !== $errno) {
34 throw new \RuntimeException(json_decode($error), $errno);
35 }
36
37 return json_decode($response);
38 }
39}
40
41class Api3CommasBots {
42
43 protected $domain = 'https://api.3commas.io';
44
45 protected $endpoint = '/public/api';
46
47 protected $url = '';
48
49 protected $key;
50
51 protected $secret;
52
53 public function __construct(string $key , string $secret) {
54 $this->key = $key;
55 $this->secret = $secret;
56 }
57
58 public function getHashedSig($url){
59 return hash_hmac('sha256', $url, $this->secret);
60 }
61
62 public function callApiGetSigned($url){
63 return $this->callApiSigned($url,'GET');
64 }
65
66 private function callApiSigned($url ,$method){
67
68 $call = $this->endpoint.$url;
69
70 $signature = $this->getHashedSig($call);
71
72 $opt = [
73 'http' => [
74 'method' => $method,
75 'header' => 'APIKEY: '. $this->key . PHP_EOL
76 .'Signature: '. $signature. PHP_EOL
77 ]
78 ];
79
80 return json_decode(
81 file_get_contents($this->domain.$call,false, stream_context_create($opt))
82 );
83 }
84
85 /**
86 * * User bots
87 * (Permission: BOTS_READ, Security: SIGNED)
88 * @param int $limit
89 * @param int|null $offset
90 *
91 * @return mixed
92 */
93 public function getBots(int $limit = 50, ?int $offset = null)
94 {
95 $url = '/ver1/bots?limit='.$limit;
96 if(!is_null($offset))
97 $url .= '&offset='.$offset;
98
99 $resultArr = $this->callApiGetSigned($url);
100 $return = [];
101 foreach ($resultArr as $pos=>$bot){
102 $return[$pos] = $bot;
103 }
104
105 return $return;
106
107 }
108
109}
110
111$key = 'YOUR_API_KEY';
112$secret = 'YOUR_API_SECRET';
113
114$api = new Api3CommasBots($key, $secret);
115var_dump($api->getBots());