Source for file curl.php
Documentation is available at curl.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* HTTP transport class for using cURL.
* @package Joomla.Platform
* @var JRegistry The client options.
* Constructor. CURLOPT_FOLLOWLOCATION must be disabled when open_basedir or safe_mode are enabled.
* @param JRegistry $options Client options object.
* @see http://www.php.net/manual/en/function.curl-setopt.php
* @throws RuntimeException
throw
new RuntimeException('Cannot use a cURL transport when curl_init() is not available.');
* Send a request to the server and return a JHttpResponse object with the response.
* @param string $method The HTTP method for sending the request.
* @param JUri $uri The URI to the resource to request.
* @param mixed $data Either an associative array or a string to be sent with the request.
* @param array $headers An array of request headers to send with the request.
* @param integer $timeout Read timeout in seconds.
* @param string $userAgent The optional user agent string to send with the request.
* @throws RuntimeException
public function request($method, JUri $uri, $data =
null, array $headers =
null, $timeout =
null, $userAgent =
null)
// Setup the cURL handle.
// Set the request method.
$options[CURLOPT_CUSTOMREQUEST] =
strtoupper($method);
// Don't wait for body when $method is HEAD
$options[CURLOPT_NOBODY] =
($method ===
'HEAD');
// Initialize the certificate store
$options[CURLOPT_CAINFO] =
$this->options->get('curl.certpath', __DIR__ .
'/cacert.pem');
// If data exists let's encode it and make sure our Content-type header is set.
// If the data is a scalar value simply add it to the cURL post fields.
if (is_scalar($data) ||
(isset
($headers['Content-Type']) &&
strpos($headers['Content-Type'], 'multipart/form-data') ===
0))
$options[CURLOPT_POSTFIELDS] =
$data;
// Otherwise we need to encode the value first.
if (!isset
($headers['Content-Type']))
$headers['Content-Type'] =
'application/x-www-form-urlencoded; charset=utf-8';
// Add the relevant headers.
$headers['Content-Length'] =
strlen($options[CURLOPT_POSTFIELDS]);
// Build the headers string for the request.
foreach ($headers as $key =>
$value)
$headerArray[] =
$key .
': ' .
$value;
// Add the headers string into the stream context options array.
$options[CURLOPT_HTTPHEADER] =
$headerArray;
// If an explicit timeout is given user it.
$options[CURLOPT_TIMEOUT] = (int)
$timeout;
$options[CURLOPT_CONNECTTIMEOUT] = (int)
$timeout;
// If an explicit user agent is given use it.
$options[CURLOPT_USERAGENT] =
$userAgent;
$options[CURLOPT_URL] = (string)
$uri;
// We want our headers. :-)
$options[CURLOPT_HEADER] =
true;
// Return it... echoing it would be tacky.
$options[CURLOPT_RETURNTRANSFER] =
true;
// Override the Expect header to prevent cURL from confusing itself in its own stupidity.
// Link: http://the-stickman.com/web-development/php-and-curl-disabling-100-continue-header/
$options[CURLOPT_HTTPHEADER][] =
'Expect:';
$options[CURLOPT_FOLLOWLOCATION] = (bool)
$this->options->get('follow_location', true);
// Execute the request and close the connection.
// Check if the content is a string. If it is not, it must be an error.
// Error but nothing from cURL? Create our own
$message =
'No HTTP response received';
throw
new RuntimeException($message);
// Get the request information.
* Method to get a response object from a server response.
* @param string $content The complete server response, including headers
* as a string if the response has no errors.
* @param array $info The cURL request information.
* @throws UnexpectedValueException
// Create the response object.
// Get the number of redirects that occurred.
$redirects = isset
($info['redirect_count']) ?
$info['redirect_count'] :
0;
* Split the response into headers and body. If cURL encountered redirects, the headers for the redirected requests will
* also be included. So we split the response into header + body + the number of redirects and only use the last two
* sections which should be the last set of headers and the actual body.
$response =
explode("\r\n\r\n", $content, 2 +
$redirects);
// Set the body for the response.
// Get the last set of response headers as an array.
// Get the response code from the first offset of the response headers.
$code =
count($matches) ?
$matches[0] :
null;
$return->code = (int)
$code;
// No valid response code was detected.
throw
new UnexpectedValueException('No HTTP response code found.');
// Add the response headers to the response object.
foreach ($headers as $header)
* Method to check if HTTP transport cURL is available for use
* @return boolean true if available, else false
Documentation generated on Tue, 19 Nov 2013 14:57:41 +0100 by phpDocumentor 1.4.3