Skip to content

Commit aa7defc

Browse files
author
Adam harding
committed
Added comments & cleaned up code
1 parent 65c9beb commit aa7defc

File tree

1 file changed

+69
-30
lines changed

1 file changed

+69
-30
lines changed

src/CryptopiaAPI.php

+69-30
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,58 @@ class CryptopiaAPI
1010
protected $curl; // curl handle
1111

1212
/**
13-
* Constructor for BinanceAPI
13+
* Constructor for CryptopiaAPI
1414
*
1515
*/
1616
function __construct()
1717
{
18+
//Initialise with the API key stored in config
1819
$this->key = config('cryptopia.auth.key');
1920
$this->secret = config('cryptopia.auth.secret');
2021
$this->url = config('cryptopia.urls.api');
22+
23+
//Initialise curl
2124
$this->curl = curl_init();
2225
curl_setopt_array($this->curl, array(
23-
CURLOPT_SSL_VERIFYPEER => true,
24-
CURLOPT_SSL_VERIFYHOST => 2,
26+
CURLOPT_SSL_VERIFYPEER => 0,
2527
CURLOPT_USERAGENT => 'Cryptopia PHP API Agent',
26-
// CURLOPT_POST => true,
27-
CURLOPT_RETURNTRANSFER => true)
28+
CURLOPT_RETURNTRANSFER => true,
29+
CURLOPT_FRESH_CONNECT => TRUE
30+
)
2831
);
2932

3033
}
3134

35+
/**
36+
* Destructor function
37+
**/
3238
function __destruct()
3339
{
3440
curl_close($this->curl);
3541
}
3642

3743

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+
**/
3850
function setAPI($key, $secret) {
3951

4052
$this->key = $key;
4153
$this->secret = $secret;
4254
}
4355

56+
57+
/**
58+
---------- PUBLIC FUNCTIONS ----------
59+
**/
60+
4461
/**
45-
* Get ticker
62+
* getTicker()
4663
*
64+
* @param $currency - optional currency to retrieve price data for, leave blank for all
4765
* @return asset pair ticker info
4866
*/
4967
public function getTicker($currency=false)
@@ -53,20 +71,35 @@ public function getTicker($currency=false)
5371
}
5472

5573

56-
public function getCurrencies($currency=false)
74+
/**
75+
* getCurrencies()
76+
* @return array of currencies available on this exchange
77+
**/
78+
public function getCurrencies()
5779
{
5880
$t = $this->request("GetCurrencies");
5981
return $t['Data'];
6082
}
6183

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()
6489
{
6590
$t = $this->request("GetTradePairs");
6691
return $t['Data'];
6792
}
6893

6994

95+
/**
96+
---------- PRIVATE ACCOUNT FUNCTIONS ----------
97+
**/
98+
99+
/**
100+
* getBalances()
101+
* @return array of currency balances for this account
102+
**/
70103
public function getBalances() {
71104

72105
$b = $this->privateRequest("GetBalance");
@@ -77,22 +110,29 @@ public function getBalances() {
77110

78111

79112

113+
/**
114+
---------- REQUESTS ----------
115+
**/
80116

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+
**/
82124
private function request($url, $params = [], $method = "GET") {
125+
83126
$opt = [
84127
"http" => [
85128
"method" => $method,
86129
"header" => "User-Agent: Mozilla/4.0 (compatible; PHP Cryptopia API)\r\n"
87130
]
88131
];
89132

90-
91-
92133
// build the POST data string
93134
$postdata = $params;
94135

95-
96136
// Set URL & Header
97137
curl_setopt($this->curl, CURLOPT_URL, $this->url . $url);
98138
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array());
@@ -111,25 +151,29 @@ private function request($url, $params = [], $method = "GET") {
111151
// decode results
112152
$result = json_decode($result, true);
113153
if(!is_array($result))
114-
throw new BinanceAPIException('JSON decode error');
154+
throw new \Exception('JSON decode error');
115155

116156
return $result;
117157

118158
}
119159

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+
**/
120168
private function privateRequest($url, $params = [], $method = "GET") {
121169

122170
$url = $this->url . $url;
123171

124172
//Doesnt work with an empty params array...
125173
if(sizeof($params)==0) $params['a'] = 'b';
126174

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];
133177
$post_data = json_encode( $params );
134178
$m = md5( $post_data, true );
135179
$requestContentBase64String = base64_encode( $m );
@@ -138,19 +182,14 @@ private function privateRequest($url, $params = [], $method = "GET") {
138182
$header_value = "amx " . $this->key . ":" . $hmacsignature . ":" . $nonce;
139183
$headers = array("Content-Type: application/json; charset=utf-8", "Authorization: $header_value");
140184

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);
150189

151190

152191
if($result===false)
153-
throw new \Exception('CURL error: ' . curl_error($ch));
192+
throw new \Exception('CURL error: ' . curl_error($this->curl));
154193

155194
// decode results
156195
$result = json_decode($result, true);

0 commit comments

Comments
 (0)