Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CurlTransport.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class CurlTransport implements CurlInterface
15 {
21  protected $config = [];
22 
28  protected $resource;
29 
35  protected $allowedParams = [
36  'timeout' => CURLOPT_TIMEOUT,
37  'maxredirects' => CURLOPT_MAXREDIRS,
38  'proxy' => CURLOPT_PROXY,
39  'ssl_cert' => CURLOPT_SSLCERT,
40  'userpwd' => CURLOPT_USERPWD,
41  ];
42 
48  protected $options = [];
49 
55  const SUCCESSFUL_HTTP_CODES = [200, 201, 202, 203, 204, 205];
56 
62  protected function applyConfig()
63  {
64  // apply additional options to cURL
65  foreach ($this->options as $option => $value) {
66  curl_setopt($this->getResource(), $option, $value);
67  }
68 
69  if (empty($this->config)) {
70  return $this;
71  }
72  foreach (array_keys($this->config) as $param) {
73  if (array_key_exists($param, $this->allowedParams)) {
74  curl_setopt($this->getResource(), $this->allowedParams[$param], $this->config[$param]);
75  }
76  }
77  return $this;
78  }
79 
86  public function setOptions(array $options = [])
87  {
88  $this->options = $options;
89  return $this;
90  }
91 
99  public function addOption($option, $value)
100  {
101  $this->options[$option] = $value;
102  return $this;
103  }
104 
111  public function setConfig(array $config = [])
112  {
113  $this->config = $config;
114  return $this;
115  }
116 
127  public function write($url, $body = [], $method = CurlInterface::POST, $headers = [])
128  {
129  $this->applyConfig();
130  $options = [
131  CURLOPT_URL => $url,
132  CURLOPT_RETURNTRANSFER => true,
133  CURLOPT_FOLLOWLOCATION => true,
134  CURLOPT_COOKIEFILE => '',
135  CURLOPT_HTTPHEADER => $headers,
136  CURLOPT_SSL_VERIFYPEER => false,
137  CURLOPT_SSL_VERIFYHOST => false,
138  ];
139  switch ($method) {
140  case CurlInterface::POST:
141  $options[CURLOPT_POST] = true;
142  $options[CURLOPT_POSTFIELDS] = $body;
143  break;
144  case CurlInterface::PUT:
145  $options[CURLOPT_CUSTOMREQUEST] = self::PUT;
146  $options[CURLOPT_POSTFIELDS] = $body;
147  break;
149  $options[CURLOPT_CUSTOMREQUEST] = self::DELETE;
150  break;
151  case CurlInterface::GET:
152  $options[CURLOPT_HTTPGET] = true;
153  break;
154  default:
155  throw new TestFrameworkException("Undefined curl method: $method");
156  }
157 
158  curl_setopt_array($this->getResource(), $options);
159  }
160 
169  public function read($successRegex = null, $returnRegex = null)
170  {
171  $response = curl_exec($this->getResource());
172 
173  if ($response === false) {
174  throw new TestFrameworkException(curl_error($this->getResource()));
175  }
176  $http_code = $this->getInfo(CURLINFO_HTTP_CODE);
177  if (!in_array($http_code, self::SUCCESSFUL_HTTP_CODES)) {
178  throw new TestFrameworkException('Error HTTP response code: ' . $http_code . ' Response:' . $response);
179  }
180 
181  return $response;
182  }
183 
189  public function close()
190  {
191  curl_close($this->getResource());
192  $this->resource = null;
193  }
194 
200  protected function getResource()
201  {
202  if ($this->resource === null) {
203  $this->resource = curl_init();
204  }
205  return $this->resource;
206  }
207 
213  public function getErrno()
214  {
215  return curl_errno($this->getResource());
216  }
217 
223  public function getError()
224  {
225  return curl_error($this->getResource());
226  }
227 
234  public function getInfo($opt = 0)
235  {
236  return curl_getinfo($this->getResource(), $opt);
237  }
238 
246  public function multiRequest(array $urls, array $options = [])
247  {
248  $handles = [];
249  $result = [];
250 
251  $multiHandle = curl_multi_init();
252 
253  foreach ($urls as $key => $url) {
254  $handles[$key] = curl_init();
255  curl_setopt($handles[$key], CURLOPT_URL, $url);
256  curl_setopt($handles[$key], CURLOPT_HEADER, 0);
257  curl_setopt($handles[$key], CURLOPT_RETURNTRANSFER, 1);
258  if (!empty($options)) {
259  curl_setopt_array($handles[$key], $options);
260  }
261  curl_multi_add_handle($multiHandle, $handles[$key]);
262  }
263  $process = null;
264  do {
265  curl_multi_exec($multiHandle, $process);
266  usleep(100);
267  } while ($process > 0);
268 
269  foreach ($handles as $key => $handle) {
270  $result[$key] = curl_multi_getcontent($handle);
271  curl_multi_remove_handle($multiHandle, $handle);
272  }
273  curl_multi_close($multiHandle);
274  return $result;
275  }
276 
283  public static function extractCode($responseStr)
284  {
285  preg_match("|^HTTP/[\d\.x]+ (\d+)|", $responseStr, $m);
286 
287  if (isset($m[1])) {
288  return (int)$m[1];
289  } else {
290  return false;
291  }
292  }
293 }
$response
Definition: 404.php:11
write($url, $body=[], $method=CurlInterface::POST, $headers=[])
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
$handle