1
1
<?php
2
2
3
3
namespace CryptAPI ;
4
+
4
5
use Exception ;
5
6
6
- class CryptAPI {
7
- private static $ base_url = "https://api.cryptapi.io " ;
8
- private static $ pro_url = "https://pro-api.cryptapi.io " ;
9
- private $ valid_coins = [];
10
- private $ own_address = null ;
7
+ class CryptAPI
8
+ {
9
+ private static $ base_url = 'https://api.cryptapi.io ' ;
10
+ private static $ pro_url = 'https://pro-api.cryptapi.io ' ;
11
+ private $ valid_coins = [];
12
+ private $ own_address = null ;
11
13
private $ payment_address = null ;
12
- private $ callback_url = null ;
13
- private $ coin = null ;
14
- private $ ca_params = [];
15
- private $ parameters = [];
16
- private $ api_key = null ;
14
+ private $ callback_url = null ;
15
+ private $ coin = null ;
16
+ private $ ca_params = [];
17
+ private $ parameters = [];
18
+ private $ api_key = null ;
17
19
18
20
public static $ COIN_MULTIPLIERS = [
19
- 'btc ' => 10 ** 8 ,
20
- 'bch ' => 10 ** 8 ,
21
- 'ltc ' => 10 ** 8 ,
22
- 'eth ' => 10 ** 18 ,
23
- 'iota ' => 10 ** 6 ,
24
- 'xmr ' => 10 ** 12 ,
25
- 'trx ' => 10 ** 6 ,
21
+ 'btc ' => 10 ** 8 ,
22
+ 'bch ' => 10 ** 8 ,
23
+ 'ltc ' => 10 ** 8 ,
24
+ 'eth ' => 10 ** 18 ,
25
+ 'iota ' => 10 ** 6 ,
26
+ 'xmr ' => 10 ** 12 ,
27
+ 'trx ' => 10 ** 6 ,
26
28
];
27
29
28
- public function __construct ($ coin , $ own_address , $ callback_url , $ parameters =[], $ ca_params =[], $ api_key =null ) {
30
+ public function __construct ($ coin , $ own_address , $ callback_url , $ parameters = [], $ ca_params = [], $ api_key = null )
31
+ {
29
32
$ this ->valid_coins = CryptAPI::get_supported_coins ();
30
33
31
34
if (!in_array ($ coin , $ this ->valid_coins )) {
32
35
$ vc = print_r ($ this ->valid_coins , true );
33
36
throw new Exception ("Unsupported Coin: {$ coin }, Valid options are: {$ vc }" );
34
37
}
35
38
36
- $ this ->own_address = $ own_address ;
39
+ $ this ->own_address = $ own_address ;
37
40
$ this ->callback_url = $ callback_url ;
38
- $ this ->coin = $ coin ;
39
- $ this ->ca_params = $ ca_params ;
40
- $ this ->parameters = $ parameters ;
41
- $ this ->api_key = $ api_key ;
41
+ $ this ->coin = $ coin ;
42
+ $ this ->ca_params = $ ca_params ;
43
+ $ this ->parameters = $ parameters ;
44
+ $ this ->api_key = $ api_key ;
42
45
}
43
46
44
- public static function get_supported_coins () {
47
+ public static function get_supported_coins ()
48
+ {
45
49
$ info = CryptAPI::get_info (null , true );
46
50
47
51
if (empty ($ info )) {
48
52
return null ;
49
53
}
50
54
51
55
unset($ info ['fee_tiers ' ]);
52
-
56
+
53
57
$ coins = [];
54
58
55
59
foreach ($ info as $ chain => $ data ) {
@@ -68,27 +72,30 @@ public static function get_supported_coins() {
68
72
return $ coins ;
69
73
}
70
74
71
- public function get_address () {
72
- if (empty ($ this ->own_address ) || empty ($ this ->coin ) || empty ($ this ->callback_url )) return null ;
75
+ public function get_address ()
76
+ {
77
+ if (empty ($ this ->own_address ) || empty ($ this ->coin ) || empty ($ this ->callback_url )) {
78
+ return null ;
79
+ }
73
80
74
81
$ api_key = $ this ->api_key ;
75
82
76
83
$ callback_url = $ this ->callback_url ;
77
84
if (!empty ($ this ->parameters )) {
78
85
$ req_parameters = http_build_query ($ this ->parameters );
79
- $ callback_url = "{$ this ->callback_url }? {$ req_parameters }" ;
86
+ $ callback_url = "{$ this ->callback_url }? {$ req_parameters }" ;
80
87
}
81
88
82
89
if (empty ($ api_key )) {
83
90
$ ca_params = array_merge ([
84
91
'callback ' => $ callback_url ,
85
- 'address ' => $ this ->own_address ,
92
+ 'address ' => $ this ->own_address ,
86
93
], $ this ->ca_params );
87
94
} else {
88
95
$ ca_params = array_merge ([
89
- 'apikey ' => $ api_key ,
96
+ 'apikey ' => $ api_key ,
90
97
'callback ' => $ callback_url ,
91
- 'address ' => $ this ->own_address ,
98
+ 'address ' => $ this ->own_address ,
92
99
], $ this ->ca_params );
93
100
}
94
101
@@ -103,13 +110,16 @@ public function get_address() {
103
110
return null ;
104
111
}
105
112
106
- public function check_logs () {
107
- if (empty ($ this ->coin ) || empty ($ this ->callback_url )) return null ;
113
+ public function check_logs ()
114
+ {
115
+ if (empty ($ this ->coin ) || empty ($ this ->callback_url )) {
116
+ return null ;
117
+ }
108
118
109
119
$ callback_url = $ this ->callback_url ;
110
120
if (!empty ($ this ->parameters )) {
111
121
$ req_parameters = http_build_query ($ this ->parameters );
112
- $ callback_url = "{$ this ->callback_url }? {$ req_parameters }" ;
122
+ $ callback_url = "{$ this ->callback_url }? {$ req_parameters }" ;
113
123
}
114
124
115
125
$ params = [
@@ -125,15 +135,22 @@ public function check_logs() {
125
135
return null ;
126
136
}
127
137
128
- public function get_qrcode ($ value = false , $ size = false ) {
129
- if (empty ($ this ->coin )) return null ;
138
+ public function get_qrcode ($ value = false , $ size = false )
139
+ {
140
+ if (empty ($ this ->coin )) {
141
+ return null ;
142
+ }
130
143
131
144
$ params = [
132
145
'address ' => $ this ->payment_address ,
133
146
];
134
147
135
- if ($ value ) $ params ['value ' ] = $ value ;
136
- if ($ size ) $ params ['size ' ] = $ size ;
148
+ if ($ value ) {
149
+ $ params ['value ' ] = $ value ;
150
+ }
151
+ if ($ size ) {
152
+ $ params ['size ' ] = $ size ;
153
+ }
137
154
138
155
$ response = CryptAPI::_request ($ this ->coin , 'qrcode ' , $ params );
139
156
@@ -144,7 +161,8 @@ public function get_qrcode($value = false, $size = false) {
144
161
return null ;
145
162
}
146
163
147
- public static function get_info ($ coin = null , $ assoc = false ) {
164
+ public static function get_info ($ coin = null , $ assoc = false )
165
+ {
148
166
$ params = [];
149
167
150
168
if (empty ($ coin )) {
@@ -160,10 +178,11 @@ public static function get_info($coin = null, $assoc = false) {
160
178
return null ;
161
179
}
162
180
163
- public static function get_estimate ($ coin , $ addresses = 1 , $ priority = 'default ' ) {
181
+ public static function get_estimate ($ coin , $ addresses = 1 , $ priority = 'default ' )
182
+ {
164
183
$ response = CryptAPI::_request ($ coin , 'estimate ' , [
165
184
'addresses ' => $ addresses ,
166
- 'priority ' => $ priority
185
+ 'priority ' => $ priority
167
186
]);
168
187
169
188
if ($ response ->status == 'success ' ) {
@@ -172,11 +191,12 @@ public static function get_estimate($coin, $addresses = 1, $priority = 'default'
172
191
173
192
return null ;
174
193
}
175
-
176
- public static function get_convert ($ coin , $ value , $ from ) {
194
+
195
+ public static function get_convert ($ coin , $ value , $ from )
196
+ {
177
197
$ response = CryptAPI::_request ($ coin , 'convert ' , [
178
198
'value ' => $ value ,
179
- 'from ' => $ from
199
+ 'from ' => $ from
180
200
]);
181
201
182
202
if ($ response ->status == 'success ' ) {
@@ -186,51 +206,58 @@ public static function get_convert($coin, $value, $from) {
186
206
return null ;
187
207
}
188
208
189
- public static function process_callback ($ _get ) {
209
+ public static function process_callback ($ _get )
210
+ {
190
211
$ params = [
191
- 'address_in ' => $ _get ['address_in ' ],
192
- 'address_out ' => $ _get ['address_out ' ],
193
- 'txid_in ' => $ _get ['txid_in ' ],
194
- 'txid_out ' => isset ($ _get ['txid_out ' ]) ? $ _get ['txid_out ' ] : null ,
195
- 'confirmations ' => $ _get ['confirmations ' ],
196
- 'value ' => $ _get ['value ' ],
197
- 'value_coin ' => $ _get ['value_coin ' ],
198
- 'value_forwarded ' => isset ($ _get ['value_forwarded ' ]) ? $ _get ['value_forwarded ' ] : null ,
212
+ 'address_in ' => $ _get ['address_in ' ],
213
+ 'address_out ' => $ _get ['address_out ' ],
214
+ 'txid_in ' => $ _get ['txid_in ' ],
215
+ 'txid_out ' => isset ($ _get ['txid_out ' ]) ? $ _get ['txid_out ' ] : null ,
216
+ 'confirmations ' => $ _get ['confirmations ' ],
217
+ 'value ' => $ _get ['value ' ],
218
+ 'value_coin ' => $ _get ['value_coin ' ],
219
+ 'value_forwarded ' => isset ($ _get ['value_forwarded ' ]) ? $ _get ['value_forwarded ' ] : null ,
199
220
'value_forwarded_coin ' => isset ($ _get ['value_forwarded_coin ' ]) ? $ _get ['value_forwarded_coin ' ] : null ,
200
- 'coin ' => $ _get ['coin ' ],
201
- 'pending ' => isset ($ _get ['pending ' ]) ? $ _get ['pending ' ] : false ,
221
+ 'coin ' => $ _get ['coin ' ],
222
+ 'pending ' => isset ($ _get ['pending ' ]) ? $ _get ['pending ' ] : false ,
202
223
];
203
224
204
225
foreach ($ _get as $ k => $ v ) {
205
- if (isset ($ params [$ k ])) continue ;
226
+ if (isset ($ params [$ k ])) {
227
+ continue ;
228
+ }
206
229
$ params [$ k ] = $ _get [$ k ];
207
230
}
208
231
209
232
return $ params ;
210
233
}
211
234
212
- private static function _request ($ coin , $ endpoint , $ params = [], $ assoc = false ) {
235
+ private static function _request ($ coin , $ endpoint , $ params = [], $ assoc = false )
236
+ {
213
237
$ base_url = Cryptapi::$ base_url ;
214
- $ coin = str_replace ('_ ' , '/ ' , $ coin );
238
+ $ coin = str_replace ('_ ' , '/ ' , ( string ) $ coin );
215
239
216
240
if (!empty ($ params ['apikey ' ]) && $ endpoint !== 'info ' ) {
217
241
$ base_url = CryptAPI::$ pro_url ;
218
242
}
219
243
220
- if (!empty ($ params )) $ data = http_build_query ($ params );
244
+ if (!empty ($ params )) {
245
+ $ data = http_build_query ($ params );
246
+ }
221
247
222
248
if (!empty ($ coin )) {
223
- $ coin = str_replace ('_ ' , '/ ' , $ coin );
224
249
$ url = "{$ base_url }/ {$ coin }/ {$ endpoint }/ " ;
225
250
} else {
226
251
$ url = "{$ base_url }/ {$ endpoint }/ " ;
227
252
}
228
253
229
- if (!empty ($ data )) $ url .= "? {$ data }" ;
254
+ if (!empty ($ data )) {
255
+ $ url .= "? {$ data }" ;
256
+ }
230
257
231
258
$ ch = curl_init ();
232
259
curl_setopt ($ ch , CURLOPT_URL , $ url );
233
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , TRUE );
260
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
234
261
$ response = curl_exec ($ ch );
235
262
curl_close ($ ch );
236
263
0 commit comments