Source for file stream.php
Documentation is available at stream.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 PHP streams.
* @package Joomla.Platform
* @var JRegistry The client options.
* @param JRegistry $options Client options object.
* @throws RuntimeException
// Verify that fopen() is available.
if (!self::isSupported())
throw
new RuntimeException('Cannot use a stream transport when fopen() is not available.');
// Verify that URLs can be used with fopen();
throw
new RuntimeException('Cannot use a stream transport when "allow_url_fopen" is disabled.');
* 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)
// Create the stream context options array with the required method offset.
$options =
array('method' =>
strtoupper($method));
// 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 stream context options.
$options['content'] =
$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['content']);
// Build the headers string for the request.
foreach ($headers as $key =>
$value)
$headerString .=
$key .
': ' .
$value .
"\r\n";
// Add the headers string into the stream context options array.
$options['header'] =
trim($headerString, "\r\n");
// If an explicit timeout is given user it.
$options['timeout'] = (int)
$timeout;
// If an explicit user agent is given use it.
$options['user_agent'] =
$userAgent;
// Ignore HTTP errors so that we can capture them.
$options['ignore_errors'] =
1;
$options['follow_location'] = (int)
$this->options->get('follow_location', 1);
// Create the stream context for the request.
$track_errors =
ini_get('track_errors');
// Open the stream for reading.
$stream =
@fopen((string)
$uri, 'r', false, $context);
// Error but nothing from php? Create our own
$php_errormsg =
sprintf('Could not connect to resource: %s', $uri, $err, $errno);
// Restore error tracking to give control to the exception handler
ini_set('track_errors', $track_errors);
throw
new RuntimeException($php_errormsg);
// Restore error tracking to what it was before.
ini_set('track_errors', $track_errors);
// Get the metadata for the stream, including response headers.
// Get the contents from the stream.
if (isset
($metadata['wrapper_data']['headers']))
$headers =
$metadata['wrapper_data']['headers'];
elseif (isset
($metadata['wrapper_data']))
$headers =
$metadata['wrapper_data'];
* Method to get a response object from a server response.
* @param array $headers The response headers as an array.
* @param string $body The response body as a string.
* @throws UnexpectedValueException
// Create the response object.
$return =
new JHttpResponse;
// Set the body for the response.
// Get the response code from the first offset of the response headers.
preg_match('/[0-9]{3}/', array_shift($headers), $matches);
$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 stream available for use
* @return bool true if available else false
Documentation generated on Tue, 19 Nov 2013 15:14:30 +0100 by phpDocumentor 1.4.3