PluginProbe
AliNext – WooCommerce Dropshipping Plugin for AliExpress / trunk
AliNext – WooCommerce Dropshipping Plugin for AliExpress vtrunk
ali2woo-lite / includes / libs / ae-php-sdk / iop / IopClient.php

IopClient.php in AliNext – WooCommerce Dropshipping Plugin for AliExpress trunk, at includes/libs/ae-php-sdk/iop/IopClient.php

321 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:ignoreFile WordPress.WP.AlternativeFunctions.curl_curl_setopt
3 class IopClient
4 {
5 public $appkey;
6
7 public $secretKey;
8
9 public $gatewayUrl;
10
11 public $connectTimeout;
12
13 public $readTimeout;
14
15 protected $signMethod = "sha256";
16
17 protected $sdkVersion = "iop-sdk-php-20220608";
18
19 public $logLevel;
20
21 public function getAppkey()
22 {
23 return $this->appkey;
24 }
25
26 public function __construct($url = "",$appkey = "",$secretKey = "")
27 {
28 $length = strlen($url);
29 if($length == 0)
30 {
31 throw new Exception("url is empty",0);
32 }
33 $this->gatewayUrl = $url;
34 $this->appkey = $appkey;
35 $this->secretKey = $secretKey;
36 $this->logLevel = Constants::$log_level_error;
37 }
38
39 protected function generateSign($apiName,$params)
40 {
41 ksort($params);
42
43 $stringToBeSigned = '';
44 if (str_contains($apiName, '/')) {//rest服务协议
45 $stringToBeSigned .= $apiName;
46 }
47 foreach ($params as $k => $v) {
48 $stringToBeSigned .= "$k$v";
49 }
50 unset($k, $v);
51
52 return strtoupper($this->hmac_sha256($stringToBeSigned,$this->secretKey));
53 }
54
55
56 function hmac_sha256($data, $key){
57 return hash_hmac('sha256', $data, $key);
58 }
59
60 public function curl_get($url,$apiFields = null,$headerFields = null)
61 {
62 // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_init
63 $ch = curl_init();
64
65 foreach ($apiFields as $key => $value)
66 {
67 $url .= "&" ."$key=" . urlencode($value);
68 }
69
70 curl_setopt($ch, CURLOPT_URL, $url);
71 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
72 curl_setopt($ch, CURLOPT_FAILONERROR, false);
73 curl_setopt($ch, CURLOPT_HEADER, false);
74 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
75
76 if ($headerFields) {
77 $headers = array();
78 foreach ($headerFields as $key => $value)
79 {
80 $headers[] = "$key: $value";
81 }
82 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
83 unset($headers);
84 }
85
86 if ($this->readTimeout) {
87 curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
88 }
89
90 if ($this->connectTimeout) {
91 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
92 }
93
94 curl_setopt ( $ch, CURLOPT_USERAGENT, $this->sdkVersion );
95
96 //https ignore ssl check ?
97 if (strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
98 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
99 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
100 }
101 // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_exec
102 $response = curl_exec($ch);
103 // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_errno
104 $errno = curl_errno($ch);
105
106 if ($errno) {
107 // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_close
108 curl_close($ch);
109 throw new Exception($errno,0);
110 }
111 else {
112 // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_getinfo
113 $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
114 // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_close
115 curl_close($ch);
116 if (200 !== $httpStatusCode)
117 {
118 throw new Exception($response, $httpStatusCode);
119 }
120 }
121
122 return $response;
123 }
124
125 public function curl_post($url, $postFields = null, $fileFields = null,$headerFields = null)
126 {
127 // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_init
128 $ch = curl_init();
129 curl_setopt($ch, CURLOPT_URL, $url);
130 curl_setopt($ch, CURLOPT_FAILONERROR, false);
131 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
132
133 if ($this->readTimeout) {
134 curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
135 }
136
137 if ($this->connectTimeout) {
138 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
139 }
140
141 if ($headerFields) {
142 $headers = array();
143 foreach ($headerFields as $key => $value)
144 {
145 $headers[] = "$key: $value";
146 }
147 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
148 unset($headers);
149 }
150
151 curl_setopt ( $ch, CURLOPT_USERAGENT, $this->sdkVersion );
152
153 //https ignore ssl check ?
154 if (strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
155 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
156 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
157 }
158
159 $delimiter = '-------------' . uniqid();
160 $data = '';
161 if ($postFields != null) {
162 foreach ($postFields as $name => $content)
163 {
164 $data .= "--" . $delimiter . "\r\n";
165 $data .= 'Content-Disposition: form-data; name="' . $name . '"';
166 $data .= "\r\n\r\n" . $content . "\r\n";
167 }
168 unset($name,$content);
169 }
170
171 if ($fileFields != null) {
172 foreach ($fileFields as $name => $file)
173 {
174 $data .= "--" . $delimiter . "\r\n";
175 $data .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $file['name'] . "\" \r\n";
176 $data .= 'Content-Type: ' . $file['type'] . "\r\n\r\n";
177 $data .= $file['content'] . "\r\n";
178 }
179 unset($name,$file);
180 }
181 $data .= "--" . $delimiter . "--";
182
183 curl_setopt($ch, CURLOPT_POST, true);
184 curl_setopt($ch, CURLOPT_HTTPHEADER ,
185 array(
186 'Content-Type: multipart/form-data; boundary=' . $delimiter,
187 'Content-Length: ' . strlen($data)
188 )
189 );
190
191 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
192 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
193 // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_exec
194 $response = curl_exec($ch);
195 unset($data);
196 // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_errno
197 $errno = curl_errno($ch);
198 if ($errno) {
199 // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_close
200 curl_close($ch);
201 throw new Exception($errno,0);
202 }
203 else
204 {
205 // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_getinfo
206 $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
207 // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_close
208 curl_close($ch);
209 if (200 !== $httpStatusCode) {
210 throw new Exception($response,$httpStatusCode);
211 }
212 }
213
214 return $response;
215 }
216
217 public function execute(IopRequest $request, $accessToken = null) {
218 $sysParams["app_key"] = $this->appkey;
219 $sysParams["sign_method"] = $this->signMethod;
220 $sysParams["timestamp"] = $this->msectime();
221 $sysParams["method"]=$request->apiName;
222 $sysParams["partner_id"] = $this->sdkVersion;
223 $sysParams["simplify"] = $request->simplify;
224 $sysParams["format"] = $request->format;
225
226 if (null != $accessToken)
227 {
228 $sysParams["session"] = $accessToken;
229 }
230
231 $apiParams = $request->udfParams;
232
233 $requestUrl = $this->gatewayUrl;
234
235 if($this->endWith($requestUrl,"/"))
236 {
237 $requestUrl = substr($requestUrl, 0, -1);
238 }
239
240 // $requestUrl .= $request->apiName;
241 $requestUrl .= '?';
242
243 if($this->logLevel == Constants::$log_level_debug)
244 {
245 $sysParams["debug"] = 'true';
246 }
247 $sysParams["sign"] = $this->generateSign($request->apiName,array_merge($apiParams, $sysParams));
248
249 foreach ($sysParams as $sysParamKey => $sysParamValue)
250 {
251 $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
252 }
253
254 $requestUrl = substr($requestUrl, 0, -1);
255
256 $resp = '';
257
258 try
259 {
260 if($request->httpMethod == 'POST')
261 {
262 $resp = $this->curl_post($requestUrl, $apiParams, $request->fileParams,$request->headerParams);
263 }
264 else
265 {
266 $resp = $this->curl_get($requestUrl, $apiParams,$request->headerParams);
267 }
268 }
269 catch (Exception $e)
270 {
271 $this->logApiError($requestUrl, $apiParams,"HTTP_ERROR_" . $e->getCode(),$e->getMessage());
272 throw $e;
273 }
274
275 unset($apiParams);
276
277 $respObject = json_decode($resp);
278 if (isset($respObject->code) && $respObject->code != "0") {
279 $this->logApiError($requestUrl, $respObject->code, $respObject->message);
280 } else {
281 if($this->logLevel == Constants::$log_level_debug || $this->logLevel == Constants::$log_level_info)
282 {
283 $this->logApiError($requestUrl, '', '');
284 }
285 }
286 return $resp;
287 }
288
289 protected function logApiError($requestUrl, $apiParams, $errorCode, $responseTxt) {
290 $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI";
291 $logger = new IopLogger;
292 $logData = array(
293 gmdate("Y-m-d H:i:s"),
294 $this->appkey,
295 $localIp,
296 PHP_OS,
297 $this->sdkVersion,
298 $requestUrl,
299 $errorCode,
300 str_replace("\n","",$responseTxt),
301 print_r($apiParams, true)
302 );
303 $logger->log($logData);
304 }
305
306 function msectime() {
307 list($msec, $sec) = explode(' ', microtime());
308 return $sec . '000';
309 }
310
311 function endWith($haystack, $needle) {
312 $length = strlen($needle);
313 if($length == 0)
314 {
315 return false;
316 }
317 return (substr($haystack, -$length) === $needle);
318 }
319
320 }
321