@@ -20,12 +20,17 @@ class RestApi
20
20
/**
21
21
* @var string
22
22
*/
23
- private $ strApiSecret ;
23
+ private $ apiSecret ;
24
24
25
25
/**
26
26
* @var string
27
27
*/
28
- private $ strApiKey ;
28
+ private $ apiKey ;
29
+
30
+ /**
31
+ * @var string
32
+ */
33
+ private $ token ;
29
34
30
35
private $ response = null ;
31
36
private $ rawResponse = null ;
@@ -98,8 +103,8 @@ public function getHttpCode()
98
103
*/
99
104
public function setApiKey ($ apiKey )
100
105
{
101
- $ this ->strApiKey = $ apiKey ;
102
-
106
+ $ this ->apiKey = $ apiKey ;
107
+ $ this -> token = '' ;
103
108
return $ this ;
104
109
}
105
110
@@ -112,8 +117,20 @@ public function setApiKey($apiKey)
112
117
*/
113
118
public function setApiSecret ($ apiSecret )
114
119
{
115
- $ this ->strApiSecret = $ apiSecret ;
120
+ $ this ->apiSecret = $ apiSecret ;
121
+ $ this ->token = '' ;
122
+ return $ this ;
123
+ }
116
124
125
+ /**
126
+ * @param $token
127
+ * @return $this
128
+ */
129
+ public function setToken ($ token )
130
+ {
131
+ $ this ->token = $ token ;
132
+ $ this ->apiKey = '' ;
133
+ $ this ->apiSecret = '' ;
117
134
return $ this ;
118
135
}
119
136
@@ -141,11 +158,14 @@ public function doRequest($strUrl, $arrParams = array(), $returnRawResponse = fa
141
158
$ strPostData = http_build_query ($ arrParams );
142
159
}
143
160
144
- $ apiSignature = sha1 ($ this ->strApiKey . '/ ' . self ::PREFIX . $ strUrl . $ strPostData . $ this ->strApiSecret );
145
-
146
161
$ headers = array ();
147
- $ headers [] = 'X-Rest-ApiKey: ' . $ this ->strApiKey ;
148
- $ headers [] = 'X-Rest-ApiSign: ' . $ apiSignature ;
162
+ if ($ this ->token ) {
163
+ $ headers [] = sprintf ('Authorization: Bearer %s ' , $ this ->token );
164
+ } elseif ($ this ->apiKey ) {
165
+ $ apiSignature = sha1 ($ this ->apiKey . '/ ' . self ::PREFIX . $ strUrl . $ strPostData . $ this ->apiSecret );
166
+ $ headers [] = sprintf ('X-Rest-ApiKey: %s ' , $ this ->apiKey );
167
+ $ headers [] = sprintf ('X-Rest-ApiSign: %s ' , $ apiSignature );
168
+ }
149
169
150
170
if (!empty ($ this ->contentType )) {
151
171
$ headers [] = 'Content-Type: ' . $ this ->contentType ;
@@ -206,20 +226,4 @@ private function getResponseFromHeaders($cUrl)
206
226
$ this ->response = json_decode (substr ($ this ->rawResponse , $ headerSize ), true );
207
227
}
208
228
}
209
-
210
- /**
211
- * Calculate request signature.
212
- *
213
- * @param string $apiKey
214
- * @param string $address
215
- * @param string $getData
216
- * @param string $postData
217
- * @param string $apiSecret
218
- *
219
- * @return string
220
- */
221
- private function calculateSignature ($ apiKey , $ address , $ getData , $ postData , $ apiSecret )
222
- {
223
- return sha1 ($ apiKey . $ address . $ getData . $ postData . $ apiSecret );
224
- }
225
229
}
0 commit comments