Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Curl Class Reference
Inheritance diagram for Curl:
ClientInterface Curl

Public Member Functions

 setTimeout ($value)
 
 __construct ($sslVersion=null)
 
 setHeaders ($headers)
 
 addHeader ($name, $value)
 
 removeHeader ($name)
 
 setCredentials ($login, $pass)
 
 addCookie ($name, $value)
 
 removeCookie ($name)
 
 setCookies ($cookies)
 
 removeCookies ()
 
 get ($uri)
 
 post ($uri, $params)
 
 getHeaders ()
 
 getBody ()
 
 getCookies ()
 
 getCookiesFull ()
 
 getStatus ()
 
 doError ($string)
 
 setOptions ($arr)
 
 setOption ($name, $value)
 

Protected Member Functions

 makeRequest ($method, $uri, $params=[])
 
 parseHeaders ($ch, $data)
 
 curlOption ($name, $value)
 
 curlOptions ($arr)
 

Protected Attributes

 $_host = 'localhost'
 
 $_port = 80
 
 $_sock = null
 
 $_headers = []
 
 $_postFields = []
 
 $_cookies = []
 
 $_responseHeaders = []
 
 $_responseBody = ''
 
 $_responseStatus = 0
 
 $_timeout = 300
 
 $_redirectCount = 0
 
 $_ch
 
 $_curlUserOptions = []
 
 $_headerCount = 0
 

Detailed Description

Class to work with HTTP protocol using curl library

Author
Magento Core Team core@.nosp@m.mage.nosp@m.ntoco.nosp@m.mmer.nosp@m.ce.co.nosp@m.m @SuppressWarnings(PHPMD.ExcessiveClassComplexity)

Definition at line 14 of file Curl.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $sslVersion = null)
Parameters
int | null$sslVersion

Definition at line 123 of file Curl.php.

124  {
125  $this->sslVersion = $sslVersion;
126  }

Member Function Documentation

◆ addCookie()

addCookie (   $name,
  $value 
)

Add cookie

Parameters
string$name
string$value
Returns
void

Implements ClientInterface.

Definition at line 183 of file Curl.php.

184  {
185  $this->_cookies[$name] = $value;
186  }
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ addHeader()

addHeader (   $name,
  $value 
)

Add header

Parameters
string$namename, ex. "Location"
string$valuevalue ex. "http://google.com"
Returns
void

Implements ClientInterface.

Definition at line 146 of file Curl.php.

147  {
148  $this->_headers[$name] = $value;
149  }
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ curlOption()

curlOption (   $name,
  $value 
)
protected

Set curl option directly

Parameters
string$name
string$value
Returns
void

Definition at line 470 of file Curl.php.

471  {
472  curl_setopt($this->_ch, $name, $value);
473  }
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ curlOptions()

curlOptions (   $arr)
protected

Set curl options array directly

Parameters
array$arr
Returns
void

Definition at line 480 of file Curl.php.

481  {
482  curl_setopt_array($this->_ch, $arr);
483  }

◆ doError()

doError (   $string)

Throw error exception

Parameters
string$string
Returns
void
Exceptions

Definition at line 417 of file Curl.php.

418  {
419  throw new \Exception($string);
420  }

◆ get()

get (   $uri)

Make GET request

Parameters
string$uriuri relative to host, ex. "/index.php"
Returns
void

Implements ClientInterface.

Definition at line 225 of file Curl.php.

226  {
227  $this->makeRequest("GET", $uri);
228  }
makeRequest($method, $uri, $params=[])
Definition: Curl.php:352

◆ getBody()

getBody ( )

Get response body

Returns
string

Implements ClientInterface.

Definition at line 262 of file Curl.php.

263  {
264  return $this->_responseBody;
265  }

◆ getCookies()

getCookies ( )

Get cookies response hash

Returns
array

Implements ClientInterface.

Definition at line 272 of file Curl.php.

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  }
$values
Definition: options.phtml:88

◆ getCookiesFull()

getCookiesFull ( )

Get cookies array with details (domain, expire time etc)

Returns
array

Definition at line 298 of file Curl.php.

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  }
$values
Definition: options.phtml:88
$i
Definition: gallery.phtml:31

◆ getHeaders()

getHeaders ( )

Get response headers

Returns
array

Implements ClientInterface.

Definition at line 252 of file Curl.php.

253  {
255  }

◆ getStatus()

getStatus ( )

Get response status code

See also
lib\Magento\Framework\HTTP\ClientgetStatus()
Returns
int

Implements ClientInterface.

Definition at line 334 of file Curl.php.

335  {
336  return $this->_responseStatus;
337  }

◆ makeRequest()

makeRequest (   $method,
  $uri,
  $params = [] 
)
protected

Make request

String type was added to parameter $param in order to support sending JSON or XML requests. This feature was added base on Community Pull Request https://github.com/magento/magento2/pull/8373

Parameters
string$method
string$uri
array | string$params- use $params as a string in case of JSON or XML POST request.
Returns
void @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Definition at line 352 of file Curl.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$method
Definition: info.phtml:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ parseHeaders()

parseHeaders (   $ch,
  $data 
)
protected

Parse headers - CURL callback function

Parameters
resource$chcurl handle, not needed
string$data
Returns
int
Exceptions

Definition at line 431 of file Curl.php.

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  }
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ post()

post (   $uri,
  $params 
)

Make POST request

String type was added to parameter $param in order to support sending JSON or XML requests. This feature was added base on Community Pull Request https://github.com/magento/magento2/pull/8373

Parameters
string$uri
array | string$params
Returns
void
See also
\Magento\Framework\HTTP\Clientpost($uri, $params)

Implements ClientInterface.

Definition at line 242 of file Curl.php.

243  {
244  $this->makeRequest("POST", $uri, $params);
245  }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
makeRequest($method, $uri, $params=[])
Definition: Curl.php:352

◆ removeCookie()

removeCookie (   $name)

Remove cookie

Parameters
string$name
Returns
void

Implements ClientInterface.

Definition at line 194 of file Curl.php.

195  {
196  unset($this->_cookies[$name]);
197  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ removeCookies()

removeCookies ( )

Clear cookies

Returns
void

Implements ClientInterface.

Definition at line 214 of file Curl.php.

215  {
216  $this->setCookies([]);
217  }

◆ removeHeader()

removeHeader (   $name)

Remove specified header

Parameters
string$name
Returns
void

Implements ClientInterface.

Definition at line 157 of file Curl.php.

158  {
159  unset($this->_headers[$name]);
160  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ setCookies()

setCookies (   $cookies)

Set cookies array

Parameters
array$cookies
Returns
void

Implements ClientInterface.

Definition at line 205 of file Curl.php.

206  {
207  $this->_cookies = $cookies;
208  }

◆ setCredentials()

setCredentials (   $login,
  $pass 
)

Authorization: Basic header Login credentials support

Parameters
string$loginusername
string$passpassword
Returns
void

Implements ClientInterface.

Definition at line 170 of file Curl.php.

171  {
172  $val = base64_encode("{$login}:{$pass}");
173  $this->addHeader("Authorization", "Basic {$val}");
174  }

◆ setHeaders()

setHeaders (   $headers)

Set headers from hash

Parameters
array$headers
Returns
void

Implements ClientInterface.

Definition at line 134 of file Curl.php.

135  {
136  $this->_headers = $headers;
137  }

◆ setOption()

setOption (   $name,
  $value 
)

Set curl option

Parameters
string$name
string$value
Returns
void

Implements ClientInterface.

Definition at line 502 of file Curl.php.

503  {
504  $this->_curlUserOptions[$name] = $value;
505  }
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ setOptions()

setOptions (   $arr)

Set CURL options overrides array

Parameters
array$arr
Returns
void

Implements ClientInterface.

Definition at line 490 of file Curl.php.

491  {
492  $this->_curlUserOptions = $arr;
493  }

◆ setTimeout()

setTimeout (   $value)

Set request timeout, msec

Parameters
int$value
Returns
void

Implements ClientInterface.

Definition at line 115 of file Curl.php.

116  {
117  $this->_timeout = (int)$value;
118  }
$value
Definition: gender.phtml:16

Field Documentation

◆ $_ch

$_ch
protected

Definition at line 92 of file Curl.php.

◆ $_cookies

$_cookies = []
protected

Definition at line 56 of file Curl.php.

◆ $_curlUserOptions

$_curlUserOptions = []
protected

Definition at line 100 of file Curl.php.

◆ $_headerCount

$_headerCount = 0
protected

Definition at line 107 of file Curl.php.

◆ $_headers

$_headers = []
protected

Definition at line 44 of file Curl.php.

◆ $_host

$_host = 'localhost'
protected

Definition at line 26 of file Curl.php.

◆ $_port

$_port = 80
protected

Definition at line 32 of file Curl.php.

◆ $_postFields

$_postFields = []
protected

Definition at line 50 of file Curl.php.

◆ $_redirectCount

$_redirectCount = 0
protected

Definition at line 86 of file Curl.php.

◆ $_responseBody

$_responseBody = ''
protected

Definition at line 68 of file Curl.php.

◆ $_responseHeaders

$_responseHeaders = []
protected

Definition at line 62 of file Curl.php.

◆ $_responseStatus

$_responseStatus = 0
protected

Definition at line 74 of file Curl.php.

◆ $_sock

$_sock = null
protected

Definition at line 38 of file Curl.php.

◆ $_timeout

$_timeout = 300
protected

Definition at line 80 of file Curl.php.


The documentation for this class was generated from the following file: