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
7 
15 {
20  private $sslVersion;
21 
26  protected $_host = 'localhost';
27 
32  protected $_port = 80;
33 
38  protected $_sock = null;
39 
44  protected $_headers = [];
45 
50  protected $_postFields = [];
51 
56  protected $_cookies = [];
57 
62  protected $_responseHeaders = [];
63 
68  protected $_responseBody = '';
69 
74  protected $_responseStatus = 0;
75 
80  protected $_timeout = 300;
81 
86  protected $_redirectCount = 0;
87 
92  protected $_ch;
93 
100  protected $_curlUserOptions = [];
101 
107  protected $_headerCount = 0;
108 
115  public function setTimeout($value)
116  {
117  $this->_timeout = (int)$value;
118  }
119 
123  public function __construct($sslVersion = null)
124  {
125  $this->sslVersion = $sslVersion;
126  }
127 
134  public function setHeaders($headers)
135  {
136  $this->_headers = $headers;
137  }
138 
146  public function addHeader($name, $value)
147  {
148  $this->_headers[$name] = $value;
149  }
150 
157  public function removeHeader($name)
158  {
159  unset($this->_headers[$name]);
160  }
161 
170  public function setCredentials($login, $pass)
171  {
172  $val = base64_encode("{$login}:{$pass}");
173  $this->addHeader("Authorization", "Basic {$val}");
174  }
175 
183  public function addCookie($name, $value)
184  {
185  $this->_cookies[$name] = $value;
186  }
187 
194  public function removeCookie($name)
195  {
196  unset($this->_cookies[$name]);
197  }
198 
205  public function setCookies($cookies)
206  {
207  $this->_cookies = $cookies;
208  }
209 
214  public function removeCookies()
215  {
216  $this->setCookies([]);
217  }
218 
225  public function get($uri)
226  {
227  $this->makeRequest("GET", $uri);
228  }
229 
242  public function post($uri, $params)
243  {
244  $this->makeRequest("POST", $uri, $params);
245  }
246 
252  public function getHeaders()
253  {
255  }
256 
262  public function getBody()
263  {
264  return $this->_responseBody;
265  }
266 
272  public function getCookies()
273  {
274  if (empty($this->_responseHeaders['Set-Cookie'])) {
275  return [];
276  }
277  $out = [];
278  foreach ($this->_responseHeaders['Set-Cookie'] as $row) {
279  $values = explode("; ", $row);
280  $c = count($values);
281  if (!$c) {
282  continue;
283  }
284  list($key, $val) = explode("=", $values[0]);
285  if ($val === null) {
286  continue;
287  }
288  $out[trim($key)] = trim($val);
289  }
290  return $out;
291  }
292 
298  public function getCookiesFull()
299  {
300  if (empty($this->_responseHeaders['Set-Cookie'])) {
301  return [];
302  }
303  $out = [];
304  foreach ($this->_responseHeaders['Set-Cookie'] as $row) {
305  $values = explode("; ", $row);
306  $c = count($values);
307  if (!$c) {
308  continue;
309  }
310  list($key, $val) = explode("=", $values[0]);
311  if ($val === null) {
312  continue;
313  }
314  $out[trim($key)] = ['value' => trim($val)];
315  array_shift($values);
316  $c--;
317  if (!$c) {
318  continue;
319  }
320  for ($i = 0; $i < $c; $i++) {
321  list($subkey, $val) = explode("=", $values[$i]);
322  $out[trim($key)][trim($subkey)] = trim($val);
323  }
324  }
325  return $out;
326  }
327 
334  public function getStatus()
335  {
336  return $this->_responseStatus;
337  }
338 
352  protected function makeRequest($method, $uri, $params = [])
353  {
354  $this->_ch = curl_init();
355  $this->curlOption(CURLOPT_URL, $uri);
356  if ($method == 'POST') {
357  $this->curlOption(CURLOPT_POST, 1);
358  $this->curlOption(CURLOPT_POSTFIELDS, is_array($params) ? http_build_query($params) : $params);
359  } elseif ($method == "GET") {
360  $this->curlOption(CURLOPT_HTTPGET, 1);
361  } else {
362  $this->curlOption(CURLOPT_CUSTOMREQUEST, $method);
363  }
364 
365  if (count($this->_headers)) {
366  $heads = [];
367  foreach ($this->_headers as $k => $v) {
368  $heads[] = $k . ': ' . $v;
369  }
370  $this->curlOption(CURLOPT_HTTPHEADER, $heads);
371  }
372 
373  if (count($this->_cookies)) {
374  $cookies = [];
375  foreach ($this->_cookies as $k => $v) {
376  $cookies[] = "{$k}={$v}";
377  }
378  $this->curlOption(CURLOPT_COOKIE, implode(";", $cookies));
379  }
380 
381  if ($this->_timeout) {
382  $this->curlOption(CURLOPT_TIMEOUT, $this->_timeout);
383  }
384 
385  if ($this->_port != 80) {
386  $this->curlOption(CURLOPT_PORT, $this->_port);
387  }
388 
389  $this->curlOption(CURLOPT_RETURNTRANSFER, 1);
390  $this->curlOption(CURLOPT_HEADERFUNCTION, [$this, 'parseHeaders']);
391  if ($this->sslVersion !== null) {
392  $this->curlOption(CURLOPT_SSLVERSION, $this->sslVersion);
393  }
394 
395  if (count($this->_curlUserOptions)) {
396  foreach ($this->_curlUserOptions as $k => $v) {
397  $this->curlOption($k, $v);
398  }
399  }
400 
401  $this->_headerCount = 0;
402  $this->_responseHeaders = [];
403  $this->_responseBody = curl_exec($this->_ch);
404  $err = curl_errno($this->_ch);
405  if ($err) {
406  $this->doError(curl_error($this->_ch));
407  }
408  curl_close($this->_ch);
409  }
410 
417  public function doError($string)
418  {
419  throw new \Exception($string);
420  }
421 
431  protected function parseHeaders($ch, $data)
432  {
433  if ($this->_headerCount == 0) {
434  $line = explode(" ", trim($data), 3);
435  if (count($line) != 3) {
436  $this->doError("Invalid response line returned from server: " . $data);
437  }
438  $this->_responseStatus = intval($line[1]);
439  } else {
440  $name = $value = '';
441  $out = explode(": ", trim($data), 2);
442  if (count($out) == 2) {
443  $name = $out[0];
444  $value = $out[1];
445  }
446 
447  if (strlen($name)) {
448  if ("Set-Cookie" == $name) {
449  if (!isset($this->_responseHeaders[$name])) {
450  $this->_responseHeaders[$name] = [];
451  }
452  $this->_responseHeaders[$name][] = $value;
453  } else {
454  $this->_responseHeaders[$name] = $value;
455  }
456  }
457  }
458  $this->_headerCount++;
459 
460  return strlen($data);
461  }
462 
470  protected function curlOption($name, $value)
471  {
472  curl_setopt($this->_ch, $name, $value);
473  }
474 
480  protected function curlOptions($arr)
481  {
482  curl_setopt_array($this->_ch, $arr);
483  }
484 
490  public function setOptions($arr)
491  {
492  $this->_curlUserOptions = $arr;
493  }
494 
502  public function setOption($name, $value)
503  {
504  $this->_curlUserOptions[$name] = $value;
505  }
506 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct($sslVersion=null)
Definition: Curl.php:123
$values
Definition: options.phtml:88
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$i
Definition: gallery.phtml:31
makeRequest($method, $uri, $params=[])
Definition: Curl.php:352
setCredentials($login, $pass)
Definition: Curl.php:170
if(!isset($_GET['name'])) $name
Definition: log.php:14