Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Curl.php
Go to the documentation of this file.
1 <?php
13 
15 {
21  protected $_config = [
22  'protocols' => (CURLPROTO_HTTP
23  | CURLPROTO_HTTPS
24  | CURLPROTO_FTP
25  | CURLPROTO_FTPS
26  ),
27  'verifypeer' => true,
28  'verifyhost' => 2
29  ];
30 
36  protected $_resource;
37 
43  protected $_allowedParams = [
44  'timeout' => CURLOPT_TIMEOUT,
45  'maxredirects' => CURLOPT_MAXREDIRS,
46  'proxy' => CURLOPT_PROXY,
47  'ssl_cert' => CURLOPT_SSLCERT,
48  'userpwd' => CURLOPT_USERPWD,
49  'useragent' => CURLOPT_USERAGENT,
50  'referer' => CURLOPT_REFERER,
51  'protocols' => CURLOPT_PROTOCOLS,
52  'verifypeer' => CURLOPT_SSL_VERIFYPEER,
53  'verifyhost' => CURLOPT_SSL_VERIFYHOST,
54  'sslversion' => CURLOPT_SSLVERSION,
55  ];
56 
62  protected $_options = [];
63 
69  protected function _applyConfig()
70  {
71  // apply additional options to cURL
72  foreach ($this->_options as $option => $value) {
73  curl_setopt($this->_getResource(), $option, $value);
74  }
75 
76  // apply config options
77  foreach ($this->getDefaultConfig() as $option => $value) {
78  curl_setopt($this->_getResource(), $option, $value);
79  }
80 
81  return $this;
82  }
83 
89  private function getDefaultConfig()
90  {
91  $config = [];
92  foreach (array_keys($this->_config) as $param) {
93  if (array_key_exists($param, $this->_allowedParams)) {
94  $config[$this->_allowedParams[$param]] = $this->_config[$param];
95  }
96  }
97  return $config;
98  }
99 
106  public function setOptions(array $options = [])
107  {
108  $this->_options = $options;
109  return $this;
110  }
111 
119  public function addOption($option, $value)
120  {
121  $this->_options[$option] = $value;
122  return $this;
123  }
124 
131  public function setConfig($config = [])
132  {
133  foreach ($config as $key => $value) {
134  $this->_config[$key] = $value;
135  }
136  return $this;
137  }
138 
148  public function connect($host, $port = 80, $secure = false)
149  {
150  return $this->_applyConfig();
151  }
152 
164  public function write($method, $url, $http_ver = '1.1', $headers = [], $body = '')
165  {
166  $this->_applyConfig();
167 
168  // set url to post to
169  curl_setopt($this->_getResource(), CURLOPT_URL, $url);
170  curl_setopt($this->_getResource(), CURLOPT_RETURNTRANSFER, true);
172  curl_setopt($this->_getResource(), CURLOPT_POST, true);
173  curl_setopt($this->_getResource(), CURLOPT_CUSTOMREQUEST, 'POST');
174  curl_setopt($this->_getResource(), CURLOPT_POSTFIELDS, $body);
176  curl_setopt($this->_getResource(), CURLOPT_CUSTOMREQUEST, 'PUT');
177  curl_setopt($this->_getResource(), CURLOPT_POSTFIELDS, $body);
179  curl_setopt($this->_getResource(), CURLOPT_HTTPGET, true);
180  curl_setopt($this->_getResource(), CURLOPT_CUSTOMREQUEST, 'GET');
181  }
182 
183  if (is_array($headers)) {
184  curl_setopt($this->_getResource(), CURLOPT_HTTPHEADER, $headers);
185  }
186 
190  $header = isset($this->_config['header']) ? $this->_config['header'] : true;
191  curl_setopt($this->_getResource(), CURLOPT_HEADER, $header);
192 
193  return $body;
194  }
195 
201  public function read()
202  {
203  $response = curl_exec($this->_getResource());
204 
205  // Remove 100 and 101 responses headers
208  ) {
209  $response = preg_split('/^\r?$/m', $response, 2);
210  $response = trim($response[1]);
211  }
212 
213  // CUrl will handle chunked data but leave the header.
214  $response = preg_replace('/Transfer-Encoding:\s+chunked\r?\n/i', '', $response);
215 
216  return $response;
217  }
218 
224  public function close()
225  {
226  curl_close($this->_getResource());
227  $this->_resource = null;
228  return $this;
229  }
230 
236  protected function _getResource()
237  {
238  if ($this->_resource === null) {
239  $this->_resource = curl_init();
240  }
241  return $this->_resource;
242  }
243 
249  public function getErrno()
250  {
251  return curl_errno($this->_getResource());
252  }
253 
259  public function getError()
260  {
261  return curl_error($this->_getResource());
262  }
263 
270  public function getInfo($opt = 0)
271  {
272  return curl_getinfo($this->_getResource(), $opt);
273  }
274 
282  public function multiRequest($urls, $options = [])
283  {
284  $handles = [];
285  $result = [];
286 
287  $multihandle = curl_multi_init();
288 
289  // add default parameters
290  foreach ($this->getDefaultConfig() as $defaultOption => $defaultValue) {
291  if (!isset($options[$defaultOption])) {
292  $options[$defaultOption] = $defaultValue;
293  }
294  }
295 
296  foreach ($urls as $key => $url) {
297  $handles[$key] = curl_init();
298  curl_setopt($handles[$key], CURLOPT_URL, $url);
299  curl_setopt($handles[$key], CURLOPT_HEADER, 0);
300  curl_setopt($handles[$key], CURLOPT_RETURNTRANSFER, 1);
301  if (!empty($options)) {
302  curl_setopt_array($handles[$key], $options);
303  }
304  curl_multi_add_handle($multihandle, $handles[$key]);
305  }
306  $process = null;
307  do {
308  curl_multi_exec($multihandle, $process);
309  usleep(100);
310  } while ($process > 0);
311 
312  foreach ($handles as $key => $handle) {
313  $result[$key] = curl_multi_getcontent($handle);
314  curl_multi_remove_handle($multihandle, $handle);
315  }
316  curl_multi_close($multihandle);
317  return $result;
318  }
319 }
$response
Definition: 404.php:11
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$config
Definition: fraud_order.php:17
setOptions(array $options=[])
Definition: Curl.php:106
addOption($option, $value)
Definition: Curl.php:119
$value
Definition: gender.phtml:16
multiRequest($urls, $options=[])
Definition: Curl.php:282
write($method, $url, $http_ver='1.1', $headers=[], $body='')
Definition: Curl.php:164
$method
Definition: info.phtml:13
connect($host, $port=80, $secure=false)
Definition: Curl.php:148
$handle
static extractCode($response_str)
Definition: Response.php:449