PluginProbe
PostNL for WooCommerce / 4.4.0
PostNL for WooCommerce v4.4.0
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / vendor / myparcelnl / sdk / src / Helper / MyParcelCurl.php

MyParcelCurl.php in PostNL for WooCommerce 4.4.0, at vendor/myparcelnl/sdk/src/Helper/MyParcelCurl.php

323 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2 /**
3 * Curl to use in the api
4 *
5 * If you want to add improvements, please create a fork in our GitHub:
6 * https://github.com/myparcelnl
7 *
8 * @author Reindert Vetter <[email protected]>
9 * @copyright 2010-2020 MyParcel
10 * @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US CC BY-NC-ND 3.0 NL
11 * @link https://github.com/myparcelnl/sdk
12 * @since File available since Release v0.1.0
13 */
14
15 namespace MyParcelNL\Sdk\src\Helper;
16
17 /**
18 * Class MyParcelCurl
19 */
20 class MyParcelCurl
21 {
22 /**
23 * Parameters array
24 *
25 * @var array
26 */
27 protected $_config = [];
28
29 /**
30 * Curl handle
31 *
32 * @var resource
33 */
34 protected $_resource;
35
36 /**
37 * Allow parameters
38 *
39 * @var array
40 */
41 protected $_allowedParams = [
42 'timeout' => CURLOPT_TIMEOUT,
43 'maxredirects' => CURLOPT_MAXREDIRS,
44 'proxy' => CURLOPT_PROXY,
45 'ssl_cert' => CURLOPT_SSLCERT,
46 'userpwd' => CURLOPT_USERPWD
47 ];
48
49 /**
50 * Array of CURL options
51 *
52 * @var array
53 */
54 protected $_options = [];
55
56 /**
57 * Set array of additional cURL options
58 *
59 * @param array $options
60 *
61 * @return MyParcelCurl
62 */
63 public function setOptions(array $options = [])
64 {
65 $this->_options = $options;
66
67 return $this;
68 }
69
70 /**
71 * Add additional options list to curl
72 *
73 * @param array $options
74 *
75 * @return MyParcelCurl
76 */
77 public function addOptions(array $options)
78 {
79 $this->_options = $options + $this->_options;
80
81 return $this;
82 }
83
84 /**
85 * Set the configuration array for the adapter
86 *
87 * @param array $config
88 *
89 * @return MyParcelCurl
90 */
91 public function setConfig($config = [])
92 {
93 $this->_config = $config;
94
95 return $this;
96 }
97
98 /**
99 * Send request to the remote server
100 *
101 * @param string $method
102 * @param string $url
103 * @param array $headers
104 * @param string $body
105 *
106 * @return string Request as text
107 */
108 public function write($method, $url, $headers = [], $body = '')
109 {
110 if ($url instanceof Zend_Uri_Http) {
111 $url = $url->getUri();
112 }
113 $this->_applyConfig();
114
115 $header = isset($this->_config['header']) ? $this->_config['header'] : true;
116 $options = [
117 CURLOPT_URL => $url,
118 CURLOPT_RETURNTRANSFER => true,
119 CURLOPT_HEADER => $header
120 ];
121 if ($method == 'POST') {
122 $options[CURLOPT_POST] = true;
123 $options[CURLOPT_POSTFIELDS] = $body;
124 } elseif ($method == 'GET') {
125 $options[CURLOPT_HTTPGET] = true;
126 }
127 if (is_array($headers)) {
128 $options[CURLOPT_HTTPHEADER] = $headers;
129 }
130
131 curl_setopt_array($this->_getResource(), $options);
132
133 return $body;
134 }
135
136 /**
137 * Read response from server
138 *
139 * @return string
140 */
141 public function read()
142 {
143 $resource = $this->_getResource();
144 $response = curl_exec($resource);
145
146 // Check the return value of curl_exec()
147 if ($response === false) {
148 throw new \Exception(curl_error($resource), curl_errno($resource));
149 }
150
151 // Remove 100 and 101 responses headers
152 while ($this->extractCode($response) == 100 || $this->extractCode($response) == 101) {
153 $response = preg_split('/^\r?$/m', $response, 2);
154 $response = trim($response[1]);
155 }
156
157 if (stripos($response, "Transfer-Encoding: chunked\r\n")) {
158 $response = str_ireplace("Transfer-Encoding: chunked\r\n", '', $response);
159 }
160
161 return $response;
162 }
163
164 /**
165 * Close the connection to the server
166 */
167 public function close()
168 {
169 curl_close($this->_getResource());
170 $this->_resource = null;
171
172 return $this;
173 }
174
175 /**
176 * Get last error number
177 *
178 * @return int
179 */
180 public function getErrno()
181 {
182 return curl_errno($this->_getResource());
183 }
184
185 /**
186 * Get string with last error for the current session
187 *
188 * @return string
189 */
190 public function getError()
191 {
192 return curl_error($this->_getResource());
193 }
194
195 /**
196 * Get information regarding a specific transfer
197 *
198 * @param int $opt CURLINFO option
199 *
200 * @return mixed
201 */
202 public function getInfo($opt = 0)
203 {
204 if (! $opt) {
205 return curl_getinfo($this->_getResource());
206 }
207
208 return curl_getinfo($this->_getResource(), $opt);
209 }
210
211 /**
212 * Set User Agent
213 *
214 * @param string $agent
215 *
216 * @return bool
217 */
218 public function setUserAgent($agent)
219 {
220 return curl_setopt($this->_getResource(), CURLOPT_USERAGENT, (string) $agent);
221 }
222
223 /**
224 * curl_multi_* requests support
225 *
226 * @param array $urls
227 * @param array $options
228 *
229 * @return array
230 */
231 public function multiRequest($urls, $options = [])
232 {
233 $handles = [];
234 $result = [];
235
236 $multihandle = curl_multi_init();
237
238 foreach ($urls as $key => $url) {
239 $handles[$key] = curl_init();
240 curl_setopt($handles[$key], CURLOPT_URL, $url);
241 curl_setopt($handles[$key], CURLOPT_HEADER, 0);
242 curl_setopt($handles[$key], CURLOPT_RETURNTRANSFER, 1);
243 if (! empty($options)) {
244 curl_setopt_array($handles[$key], $options);
245 }
246 curl_multi_add_handle($multihandle, $handles[$key]);
247 }
248 $process = null;
249 do {
250 curl_multi_exec($multihandle, $process);
251 usleep(100);
252 } while ($process > 0);
253
254 foreach ($handles as $key => $handle) {
255 $result[$key] = curl_multi_getcontent($handle);
256 curl_multi_remove_handle($multihandle, $handle);
257 }
258 curl_multi_close($multihandle);
259
260 return $result;
261 }
262
263 /**
264 * Apply current configuration array to transport resource
265 *
266 * @return MyParcelCurl
267 */
268 protected function _applyConfig()
269 {
270 curl_setopt_array($this->_getResource(), $this->_options);
271
272 if (empty($this->_config)) {
273 return $this;
274 }
275
276 $verifyPeer = isset($this->_config['verifypeer']) ? $this->_config['verifypeer'] : 0;
277 curl_setopt($this->_getResource(), CURLOPT_SSL_VERIFYPEER, $verifyPeer);
278
279 $verifyHost = isset($this->_config['verifyhost']) ? $this->_config['verifyhost'] : 0;
280 curl_setopt($this->_getResource(), CURLOPT_SSL_VERIFYHOST, $verifyHost);
281
282 foreach ($this->_config as $param => $curlOption) {
283 if (array_key_exists($param, $this->_allowedParams)) {
284 curl_setopt($this->_getResource(), $this->_allowedParams[$param], $this->_config[$param]);
285 }
286 }
287
288 return $this;
289 }
290
291 /**
292 * Returns a cURL handle on success
293 *
294 * @return resource
295 */
296 protected function _getResource()
297 {
298 if (is_null($this->_resource)) {
299 $this->_resource = curl_init();
300 }
301
302 return $this->_resource;
303 }
304
305 /**
306 * Extract the response code from a response string
307 *
308 * @param string $response_str
309 *
310 * @return int
311 */
312 private static function extractCode($response_str)
313 {
314 preg_match("|^HTTP/[\d\.x]+ (\d+)|", $response_str, $m);
315
316 if (isset($m[1])) {
317 return (int) $m[1];
318 } else {
319 return false;
320 }
321 }
322 }
323