@@ -10,40 +10,58 @@ class CryptopiaAPI
10
10
protected $ curl ; // curl handle
11
11
12
12
/**
13
- * Constructor for BinanceAPI
13
+ * Constructor for CryptopiaAPI
14
14
*
15
15
*/
16
16
function __construct ()
17
17
{
18
+ //Initialise with the API key stored in config
18
19
$ this ->key = config ('cryptopia.auth.key ' );
19
20
$ this ->secret = config ('cryptopia.auth.secret ' );
20
21
$ this ->url = config ('cryptopia.urls.api ' );
22
+
23
+ //Initialise curl
21
24
$ this ->curl = curl_init ();
22
25
curl_setopt_array ($ this ->curl , array (
23
- CURLOPT_SSL_VERIFYPEER => true ,
24
- CURLOPT_SSL_VERIFYHOST => 2 ,
26
+ CURLOPT_SSL_VERIFYPEER => 0 ,
25
27
CURLOPT_USERAGENT => 'Cryptopia PHP API Agent ' ,
26
- // CURLOPT_POST => true,
27
- CURLOPT_RETURNTRANSFER => true )
28
+ CURLOPT_RETURNTRANSFER => true ,
29
+ CURLOPT_FRESH_CONNECT => TRUE
30
+ )
28
31
);
29
32
30
33
}
31
34
35
+ /**
36
+ * Destructor function
37
+ **/
32
38
function __destruct ()
33
39
{
34
40
curl_close ($ this ->curl );
35
41
}
36
42
37
43
44
+ /**
45
+ * setAPI()
46
+ * @param $key - API key
47
+ * @param $secret - API secret
48
+ * We can change the API key to access different accounts
49
+ **/
38
50
function setAPI ($ key , $ secret ) {
39
51
40
52
$ this ->key = $ key ;
41
53
$ this ->secret = $ secret ;
42
54
}
43
55
56
+
57
+ /**
58
+ ---------- PUBLIC FUNCTIONS ----------
59
+ **/
60
+
44
61
/**
45
- * Get ticker
62
+ * getTicker()
46
63
*
64
+ * @param $currency - optional currency to retrieve price data for, leave blank for all
47
65
* @return asset pair ticker info
48
66
*/
49
67
public function getTicker ($ currency =false )
@@ -53,20 +71,35 @@ public function getTicker($currency=false)
53
71
}
54
72
55
73
56
- public function getCurrencies ($ currency =false )
74
+ /**
75
+ * getCurrencies()
76
+ * @return array of currencies available on this exchange
77
+ **/
78
+ public function getCurrencies ()
57
79
{
58
80
$ t = $ this ->request ("GetCurrencies " );
59
81
return $ t ['Data ' ];
60
82
}
61
83
62
-
63
- public function getAssetPairs ($ currency =false )
84
+ /**
85
+ * getAssetPairs()
86
+ * @return array of trading pairs available on this exchange
87
+ **/
88
+ public function getAssetPairs ()
64
89
{
65
90
$ t = $ this ->request ("GetTradePairs " );
66
91
return $ t ['Data ' ];
67
92
}
68
93
69
94
95
+ /**
96
+ ---------- PRIVATE ACCOUNT FUNCTIONS ----------
97
+ **/
98
+
99
+ /**
100
+ * getBalances()
101
+ * @return array of currency balances for this account
102
+ **/
70
103
public function getBalances () {
71
104
72
105
$ b = $ this ->privateRequest ("GetBalance " );
@@ -77,22 +110,29 @@ public function getBalances() {
77
110
78
111
79
112
113
+ /**
114
+ ---------- REQUESTS ----------
115
+ **/
80
116
81
-
117
+ /** request()
118
+ * @param $url - append to the API url to create full request url
119
+ * @param $params - additional parameters to send
120
+ * @param $method - GET or POST
121
+ * @return array from json decoded string
122
+ * Handles the requests for publically accessible data
123
+ **/
82
124
private function request ($ url , $ params = [], $ method = "GET " ) {
125
+
83
126
$ opt = [
84
127
"http " => [
85
128
"method " => $ method ,
86
129
"header " => "User-Agent: Mozilla/4.0 (compatible; PHP Cryptopia API) \r\n"
87
130
]
88
131
];
89
132
90
-
91
-
92
133
// build the POST data string
93
134
$ postdata = $ params ;
94
135
95
-
96
136
// Set URL & Header
97
137
curl_setopt ($ this ->curl , CURLOPT_URL , $ this ->url . $ url );
98
138
curl_setopt ($ this ->curl , CURLOPT_HTTPHEADER , array ());
@@ -111,25 +151,29 @@ private function request($url, $params = [], $method = "GET") {
111
151
// decode results
112
152
$ result = json_decode ($ result , true );
113
153
if (!is_array ($ result ))
114
- throw new BinanceAPIException ('JSON decode error ' );
154
+ throw new \ Exception ('JSON decode error ' );
115
155
116
156
return $ result ;
117
157
118
158
}
119
159
160
+
161
+ /** privateRequest()
162
+ * @param $url - append to the API url to create full request url
163
+ * @param $params - additional parameters to send
164
+ * @param $method - GET or POST
165
+ * @return array from json decoded string
166
+ * Handles the private requests for account data
167
+ **/
120
168
private function privateRequest ($ url , $ params = [], $ method = "GET " ) {
121
169
122
170
$ url = $ this ->url . $ url ;
123
171
124
172
//Doesnt work with an empty params array...
125
173
if (sizeof ($ params )==0 ) $ params ['a ' ] = 'b ' ;
126
174
127
- static $ ch = null ;
128
- $ ch = curl_init ();
129
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
130
- curl_setopt ($ ch , CURLOPT_USERAGENT , 'Mozilla/4.0 (compatible; Cryptopia.co.nz API PHP client; ' .php_uname ('s ' ).'; PHP/ ' .phpversion ().') ' );
131
-
132
- $ nonce = explode (' ' , microtime ())[1 ];
175
+ //Authorisation & request code taken from PHP example on cryptopia API guide
176
+ $ nonce = explode (' ' , microtime ())[1 ];
133
177
$ post_data = json_encode ( $ params );
134
178
$ m = md5 ( $ post_data , true );
135
179
$ requestContentBase64String = base64_encode ( $ m );
@@ -138,19 +182,14 @@ private function privateRequest($url, $params = [], $method = "GET") {
138
182
$ header_value = "amx " . $ this ->key . ": " . $ hmacsignature . ": " . $ nonce ;
139
183
$ headers = array ("Content-Type: application/json; charset=utf-8 " , "Authorization: $ header_value " );
140
184
141
-
142
- curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
143
- curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ( $ params ) );
144
-
145
- curl_setopt ($ ch , CURLOPT_URL , $ url );
146
- curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , 0 );
147
- curl_setopt ($ ch , CURLOPT_FRESH_CONNECT , TRUE );
148
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
149
- $ result = curl_exec ($ ch );
185
+ curl_setopt ($ this ->curl , CURLOPT_HTTPHEADER , $ headers );
186
+ curl_setopt ($ this ->curl , CURLOPT_POSTFIELDS , json_encode ( $ params ) );
187
+ curl_setopt ($ this ->curl , CURLOPT_URL , $ url );
188
+ $ result = curl_exec ($ this ->curl );
150
189
151
190
152
191
if ($ result ===false )
153
- throw new \Exception ('CURL error: ' . curl_error ($ ch ));
192
+ throw new \Exception ('CURL error: ' . curl_error ($ this -> curl ));
154
193
155
194
// decode results
156
195
$ result = json_decode ($ result , true );
0 commit comments