Back to SocketTransport class

Method request

public \Joomla\CMS\Http\Response
request
(mixed $method, \Joomla\Uri\UriInterface $uri, mixed $data = null, array $headers = [], mixed $timeout = null, mixed $userAgent = null)
Send a request to the server and return a Response object with the response.
Parameters
  • string $method The HTTP method for sending the request.
  • \Joomla\Uri\UriInterface $uri The URI to the resource to request.
  • mixed $data Either an associative array or a string to be sent with the request.
  • array $headers An array of request headers to send with the request.
  • int $timeout Read timeout in seconds.
  • string $userAgent The optional user agent string to send with the request.
Returns
  • \Joomla\CMS\Http\Response
Since
  • 1.7.3
-
  • \RuntimeException

Method request - Source code

/**
 * Send a request to the server and return a Response object with the response.
 *
 * @param   string        $method     The HTTP method for sending the request.
 * @param   UriInterface  $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.
 *
 * @return  Response
 *
 * @since   1.7.3
 * @throws  \RuntimeException
 */
public function request($method, UriInterface $uri, $data = null, array $headers = [], $timeout = null, $userAgent = null)
{
    $connection = $this->connect($uri, $timeout);
    // Make sure the connection is alive and valid.
    if (\is_resource($connection)) {
        // Make sure the connection has not timed out.
        $meta = stream_get_meta_data($connection);
        if ($meta['timed_out']) {
            throw new \RuntimeException('Server connection timed out.');
        }
    } else {
        throw new \RuntimeException('Not connected to server.');
    }
    // Get the request path from the URI object.
    $path = $uri->toString(array('path', 'query'));
    // If we have data to send make sure our request is setup for it.
    if (!empty($data)) {
        // If the data is not a scalar value encode it to be sent with the request.
        if (!is_scalar($data)) {
            $data = http_build_query($data);
        }
        if (!isset($headers['Content-Type'])) {
            $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
        }
        // Add the relevant headers.
        $headers['Content-Length'] = \strlen($data);
    }
    // Build the request payload.
    $request = array();
    $request[] = strtoupper($method) . ' ' . (empty($path) ? '/' : $path) . ' HTTP/1.1';
    $request[] = 'Host: ' . $uri->getHost();
    // If an explicit user agent is given use it.
    if (isset($userAgent)) {
        $headers['User-Agent'] = $userAgent;
    }
    // If there are custom headers to send add them to the request payload.
    if (\is_array($headers)) {
        foreach ($headers as $k => $v) {
            $request[] = $k . ': ' . $v;
        }
    }
    // Set any custom transport options
    foreach ($this->getOption('transport.socket', array()) as $value) {
        $request[] = $value;
    }
    // If we have data to send add it to the request payload.
    if (!empty($data)) {
        $request[] = null;
        $request[] = $data;
    }
    // Authentication, if needed
    if ($this->getOption('userauth') && $this->getOption('passwordauth')) {
        $request[] = 'Authorization: Basic ' . base64_encode($this->getOption('userauth') . ':' . $this->getOption('passwordauth'));
    }
    // Send the request to the server.
    fwrite($connection, implode("\r\n", $request) . "\r\n\r\n");
    // Get the response data from the server.
    $content = '';
    while (!feof($connection)) {
        $content .= fgets($connection, 4096);
    }
    $content = $this->getResponse($content);
    // Follow Http redirects
    if ($content->code >= 301 && $content->code < 400 && isset($content->headers['Location'])) {
        return $this->request($method, new Uri($content->headers['Location']), $data, $headers, $timeout, $userAgent);
    }
    return $content;
}