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

Public Member Functions

 __construct ()
 
 setConfig ($config=array())
 
 getConfig ()
 
 setCurlOption ($option, $value)
 
 connect ($host, $port=80, $secure=false)
 
 write ($method, $uri, $httpVersion=1.1, $headers=array(), $body='')
 
 read ()
 
 close ()
 
 getHandle ()
 
 setOutputStream ($stream)
 
 readHeader ($curl, $header)
 

Protected Attributes

 $_config = array()
 
 $_connected_to = array(null, null)
 
 $_curl = null
 
 $_invalidOverwritableCurlOptions
 
 $_response = null
 
 $out_stream
 

Detailed Description

Definition at line 48 of file Curl.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( )

Adapter constructor

Config is set using setConfig()

Returns
void
Exceptions
Zend_Http_Client_Adapter_Exception

Definition at line 100 of file Curl.php.

101  {
102  if (!extension_loaded('curl')) {
103  #require_once 'Zend/Http/Client/Adapter/Exception.php';
104  throw new Zend_Http_Client_Adapter_Exception('cURL extension has to be loaded to use this Zend_Http_Client adapter.');
105  }
106  $this->_invalidOverwritableCurlOptions = array(
107  CURLOPT_HTTPGET,
108  CURLOPT_POST,
109  CURLOPT_PUT,
110  CURLOPT_CUSTOMREQUEST,
111  CURLOPT_HEADER,
112  CURLOPT_RETURNTRANSFER,
113  CURLOPT_HTTPHEADER,
114  CURLOPT_POSTFIELDS,
115  CURLOPT_INFILE,
116  CURLOPT_INFILESIZE,
117  CURLOPT_PORT,
118  CURLOPT_MAXREDIRS,
119  CURLOPT_CONNECTTIMEOUT,
120  CURL_HTTP_VERSION_1_1,
121  CURL_HTTP_VERSION_1_0,
122  );
123  }

Member Function Documentation

◆ close()

close ( )

Close the connection to the server

Implements Zend_Http_Client_Adapter_Interface.

Definition at line 496 of file Curl.php.

497  {
498  if(is_resource($this->_curl)) {
499  curl_close($this->_curl);
500  }
501  $this->_curl = null;
502  $this->_connected_to = array(null, null);
503  }

◆ connect()

connect (   $host,
  $port = 80,
  $secure = false 
)

Initialize curl

Parameters
string$host
int$port
boolean$secure
Returns
void
Exceptions
Zend_Http_Client_Adapter_Exceptionif unable to connect

Implements Zend_Http_Client_Adapter_Interface.

Definition at line 202 of file Curl.php.

203  {
204  // If we're already connected, disconnect first
205  if ($this->_curl) {
206  $this->close();
207  }
208 
209  // If we are connected to a different server or port, disconnect first
210  if ($this->_curl
211  && is_array($this->_connected_to)
212  && ($this->_connected_to[0] != $host
213  || $this->_connected_to[1] != $port)
214  ) {
215  $this->close();
216  }
217 
218  // Do the actual connection
219  $this->_curl = curl_init();
220  if ($port != 80) {
221  curl_setopt($this->_curl, CURLOPT_PORT, intval($port));
222  }
223 
224  // Set connection timeout
225  $connectTimeout = $this->_config['timeout'];
226  $constant = CURLOPT_CONNECTTIMEOUT;
227  if (defined('CURLOPT_CONNECTTIMEOUT_MS')) {
228  $connectTimeout *= 1000;
229  $constant = constant('CURLOPT_CONNECTTIMEOUT_MS');
230  }
231  curl_setopt($this->_curl, $constant, $connectTimeout);
232 
233  // Set request timeout (once connection is established)
234  if (array_key_exists('request_timeout', $this->_config)) {
235  $requestTimeout = $this->_config['request_timeout'];
236  $constant = CURLOPT_TIMEOUT;
237  if (defined('CURLOPT_TIMEOUT_MS')) {
238  $requestTimeout *= 1000;
239  $constant = constant('CURLOPT_TIMEOUT_MS');
240  }
241  curl_setopt($this->_curl, $constant, $requestTimeout);
242  }
243 
244  // Set Max redirects
245  curl_setopt($this->_curl, CURLOPT_MAXREDIRS, $this->_config['maxredirects']);
246 
247  if (!$this->_curl) {
248  $this->close();
249 
250  #require_once 'Zend/Http/Client/Adapter/Exception.php';
251  throw new Zend_Http_Client_Adapter_Exception('Unable to Connect to ' . $host . ':' . $port);
252  }
253 
254  if ($secure !== false) {
255  // Behave the same like Zend_Http_Adapter_Socket on SSL options.
256  if (isset($this->_config['sslcert'])) {
257  curl_setopt($this->_curl, CURLOPT_SSLCERT, $this->_config['sslcert']);
258  }
259  if (isset($this->_config['sslpassphrase'])) {
260  curl_setopt($this->_curl, CURLOPT_SSLCERTPASSWD, $this->_config['sslpassphrase']);
261  }
262  }
263 
264  // Update connected_to
265  $this->_connected_to = array($host, $port);
266  }

◆ getConfig()

getConfig ( )

Retrieve the array of all configuration options

Returns
array

Definition at line 172 of file Curl.php.

173  {
174  return $this->_config;
175  }

◆ getHandle()

getHandle ( )

Get cUrl Handle

Returns
resource

Definition at line 510 of file Curl.php.

511  {
512  return $this->_curl;
513  }

◆ read()

read ( )

Return read response from server

Returns
string

Implements Zend_Http_Client_Adapter_Interface.

Definition at line 487 of file Curl.php.

488  {
489  return $this->_response;
490  }

◆ readHeader()

readHeader (   $curl,
  $header 
)

Header reader function for CURL

Parameters
resource$curl
string$header
Returns
int

Definition at line 534 of file Curl.php.

535  {
536  $this->_response .= $header;
537  return strlen($header);
538  }

◆ setConfig()

setConfig (   $config = array())

Set the configuration array for the adapter

Exceptions
Zend_Http_Client_Adapter_Exception
Parameters
Zend_Config  |  array$config
Returns
Zend_Http_Client_Adapter_Curl

Implements Zend_Http_Client_Adapter_Interface.

Definition at line 132 of file Curl.php.

133  {
134  if ($config instanceof Zend_Config) {
135  $config = $config->toArray();
136 
137  } elseif (! is_array($config)) {
138  #require_once 'Zend/Http/Client/Adapter/Exception.php';
140  'Array or Zend_Config object expected, got ' . gettype($config)
141  );
142  }
143 
144  if(isset($config['proxy_user']) && isset($config['proxy_pass'])) {
145  $this->setCurlOption(CURLOPT_PROXYUSERPWD, $config['proxy_user'].":".$config['proxy_pass']);
146  unset($config['proxy_user'], $config['proxy_pass']);
147  }
148 
149  foreach ($config as $k => $v) {
150  $option = strtolower($k);
151  switch($option) {
152  case 'proxy_host':
153  $this->setCurlOption(CURLOPT_PROXY, $v);
154  break;
155  case 'proxy_port':
156  $this->setCurlOption(CURLOPT_PROXYPORT, $v);
157  break;
158  default:
159  $this->_config[$option] = $v;
160  break;
161  }
162  }
163 
164  return $this;
165  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
setCurlOption($option, $value)
Definition: Curl.php:184
$config
Definition: fraud_order.php:17

◆ setCurlOption()

setCurlOption (   $option,
  $value 
)

Direct setter for cURL adapter related options.

Parameters
string | int$option
mixed$value
Returns
Zend_Http_Adapter_Curl

Definition at line 184 of file Curl.php.

185  {
186  if (!isset($this->_config['curloptions'])) {
187  $this->_config['curloptions'] = array();
188  }
189  $this->_config['curloptions'][$option] = $value;
190  return $this;
191  }
$value
Definition: gender.phtml:16

◆ setOutputStream()

setOutputStream (   $stream)

Set output stream for the response

Parameters
resource$stream
Returns
Zend_Http_Client_Adapter_Socket

Implements Zend_Http_Client_Adapter_Stream.

Definition at line 521 of file Curl.php.

522  {
523  $this->out_stream = $stream;
524  return $this;
525  }

◆ write()

write (   $method,
  $uri,
  $httpVersion = 1.1,
  $headers = array(),
  $body = '' 
)

Send request to the remote server

Parameters
string$method
Zend_Uri_Http$uri
float$http_ver
array$headers
string$body
Returns
string $request
Exceptions
Zend_Http_Client_Adapter_ExceptionIf connection fails, connected to wrong host, no PUT file defined, unsupported method, or unsupported cURL option

Make sure POSTFIELDS is set after $curlMethod is set: http://de2.php.net/manual/en/function.curl-setopt.php#81161

Implements Zend_Http_Client_Adapter_Interface.

Definition at line 279 of file Curl.php.

280  {
281  // Make sure we're properly connected
282  if (!$this->_curl) {
283  #require_once 'Zend/Http/Client/Adapter/Exception.php';
284  throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are not connected");
285  }
286 
287  if ($this->_connected_to[0] != $uri->getHost() || $this->_connected_to[1] != $uri->getPort()) {
288  #require_once 'Zend/Http/Client/Adapter/Exception.php';
289  throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are connected to the wrong host");
290  }
291 
292  // set URL
293  curl_setopt($this->_curl, CURLOPT_URL, $uri->__toString());
294 
295  // ensure correct curl call
296  $curlValue = true;
297  switch ($method) {
299  $curlMethod = CURLOPT_HTTPGET;
300  break;
301 
303  $curlMethod = CURLOPT_POST;
304  break;
305 
307  // There are two different types of PUT request, either a Raw Data string has been set
308  // or CURLOPT_INFILE and CURLOPT_INFILESIZE are used.
309  if(is_resource($body)) {
310  $this->_config['curloptions'][CURLOPT_INFILE] = $body;
311  }
312  if (isset($this->_config['curloptions'][CURLOPT_INFILE])) {
313  // Now we will probably already have Content-Length set, so that we have to delete it
314  // from $headers at this point:
315  foreach ($headers AS $k => $header) {
316  if (preg_match('/Content-Length:\s*(\d+)/i', $header, $m)) {
317  if(is_resource($body)) {
318  $this->_config['curloptions'][CURLOPT_INFILESIZE] = (int)$m[1];
319  }
320  unset($headers[$k]);
321  }
322  }
323 
324  if (!isset($this->_config['curloptions'][CURLOPT_INFILESIZE])) {
325  #require_once 'Zend/Http/Client/Adapter/Exception.php';
326  throw new Zend_Http_Client_Adapter_Exception("Cannot set a file-handle for cURL option CURLOPT_INFILE without also setting its size in CURLOPT_INFILESIZE.");
327  }
328 
329  if(is_resource($body)) {
330  $body = '';
331  }
332 
333  $curlMethod = CURLOPT_PUT;
334  } else {
335  $curlMethod = CURLOPT_CUSTOMREQUEST;
336  $curlValue = "PUT";
337  }
338  break;
339 
341  $curlMethod = CURLOPT_CUSTOMREQUEST;
342  $curlValue = "PATCH";
343  break;
344 
346  $curlMethod = CURLOPT_CUSTOMREQUEST;
347  $curlValue = "DELETE";
348  break;
349 
351  $curlMethod = CURLOPT_CUSTOMREQUEST;
352  $curlValue = "OPTIONS";
353  break;
354 
356  $curlMethod = CURLOPT_CUSTOMREQUEST;
357  $curlValue = "TRACE";
358  break;
359 
361  $curlMethod = CURLOPT_CUSTOMREQUEST;
362  $curlValue = "HEAD";
363  break;
364 
365  default:
366  // For now, through an exception for unsupported request methods
367  #require_once 'Zend/Http/Client/Adapter/Exception.php';
368  throw new Zend_Http_Client_Adapter_Exception("Method currently not supported");
369  }
370 
371  if(is_resource($body) && $curlMethod != CURLOPT_PUT) {
372  #require_once 'Zend/Http/Client/Adapter/Exception.php';
373  throw new Zend_Http_Client_Adapter_Exception("Streaming requests are allowed only with PUT");
374  }
375 
376  // get http version to use
377  $curlHttp = ($httpVersion == 1.1) ? CURL_HTTP_VERSION_1_1 : CURL_HTTP_VERSION_1_0;
378 
379  // mark as HTTP request and set HTTP method
380  curl_setopt($this->_curl, CURLOPT_HTTP_VERSION, $curlHttp);
381  curl_setopt($this->_curl, $curlMethod, $curlValue);
382 
383  if($this->out_stream) {
384  // headers will be read into the response
385  curl_setopt($this->_curl, CURLOPT_HEADER, false);
386  curl_setopt($this->_curl, CURLOPT_HEADERFUNCTION, array($this, "readHeader"));
387  // and data will be written into the file
388  curl_setopt($this->_curl, CURLOPT_FILE, $this->out_stream);
389  } else {
390  // ensure headers are also returned
391  curl_setopt($this->_curl, CURLOPT_HEADER, true);
392  curl_setopt($this->_curl, CURLINFO_HEADER_OUT, true);
393 
394  // ensure actual response is returned
395  curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, true);
396  }
397 
398  // set additional headers
399  $headers['Accept'] = '';
400  curl_setopt($this->_curl, CURLOPT_HTTPHEADER, $headers);
401 
407  curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
408  } elseif ($curlMethod == CURLOPT_PUT) {
409  // this covers a PUT by file-handle:
410  // Make the setting of this options explicit (rather than setting it through the loop following a bit lower)
411  // to group common functionality together.
412  curl_setopt($this->_curl, CURLOPT_INFILE, $this->_config['curloptions'][CURLOPT_INFILE]);
413  curl_setopt($this->_curl, CURLOPT_INFILESIZE, $this->_config['curloptions'][CURLOPT_INFILESIZE]);
414  unset($this->_config['curloptions'][CURLOPT_INFILE]);
415  unset($this->_config['curloptions'][CURLOPT_INFILESIZE]);
417  // This is a PUT by a setRawData string, not by file-handle
418  curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
420  // This is a PATCH by a setRawData string
421  curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
423  // This is a DELETE by a setRawData string
424  curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
426  // This is an OPTIONS by a setRawData string
427  curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
428  }
429 
430  // set additional curl options
431  if (isset($this->_config['curloptions'])) {
432  foreach ((array)$this->_config['curloptions'] as $k => $v) {
433  if (!in_array($k, $this->_invalidOverwritableCurlOptions)) {
434  if (curl_setopt($this->_curl, $k, $v) == false) {
435  #require_once 'Zend/Http/Client/Exception.php';
436  throw new Zend_Http_Client_Exception(sprintf("Unknown or erroreous cURL option '%s' set", $k));
437  }
438  }
439  }
440  }
441 
442  // send the request
443  $response = curl_exec($this->_curl);
444 
445  // if we used streaming, headers are already there
446  if(!is_resource($this->out_stream)) {
447  $this->_response = $response;
448  }
449 
450  $request = curl_getinfo($this->_curl, CURLINFO_HEADER_OUT);
451  $request .= $body;
452 
453  if (empty($this->_response)) {
454  #require_once 'Zend/Http/Client/Exception.php';
455  throw new Zend_Http_Client_Exception("Error in cURL request: " . curl_error($this->_curl));
456  }
457 
458  // cURL automatically decodes chunked-messages, this means we have to disallow the Zend_Http_Response to do it again
459  if (stripos($this->_response, "Transfer-Encoding: chunked\r\n")) {
460  $this->_response = str_ireplace("Transfer-Encoding: chunked\r\n", '', $this->_response);
461  }
462 
463  // Eliminate multiple HTTP responses.
464  do {
465  $parts = preg_split('|(?:\r?\n){2}|m', $this->_response, 2);
466  $again = false;
467 
468  if (isset($parts[1]) && preg_match("|^HTTP/1\.[01](.*?)\r\n|mi", $parts[1])) {
469  $this->_response = $parts[1];
470  $again = true;
471  }
472  } while ($again);
473 
474  // cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string:
475  if (stripos($this->_response, "HTTP/1.0 200 Connection established\r\n\r\n") !== false) {
476  $this->_response = str_ireplace("HTTP/1.0 200 Connection established\r\n\r\n", '', $this->_response);
477  }
478 
479  return $request;
480  }
$response
Definition: 404.php:11
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$method
Definition: info.phtml:13
const OPTIONS
Definition: Client.php:83
const DELETE
Definition: Client.php:81

Field Documentation

◆ $_config

$_config = array()
protected

Definition at line 55 of file Curl.php.

◆ $_connected_to

$_connected_to = array(null, null)
protected

Definition at line 62 of file Curl.php.

◆ $_curl

$_curl = null
protected

Definition at line 69 of file Curl.php.

◆ $_invalidOverwritableCurlOptions

$_invalidOverwritableCurlOptions
protected

Definition at line 76 of file Curl.php.

◆ $_response

$_response = null
protected

Definition at line 83 of file Curl.php.

◆ $out_stream

$out_stream
protected

Definition at line 90 of file Curl.php.


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