Modules

Request_Client_Stream
extends Kohana_Request_Client_Stream
extends Request_Client_External
extends Kohana_Request_Client_External
extends Request_Client
extends Kohana_Request_Client

Request_Client_External Stream driver performs external requests using php sockets. To use this driver, ensure the following is completed before executing an external request- ideally in the application bootstrap.

// In application bootstrap
Request_Client_External::$client = 'Request_Client_Stream';
example
package
Kohana
category
Base
author
Kohana Team
copyright
© 2008-2012 Kohana Team
license
http://kohanaframework.org/license
uses
[PHP Streams](http://php.net/manual/en/book.stream.php)

Class declared in SYSPATH/classes/request/client/stream.php on line 3.

Properties

public static string $client

defines the external client to use by default

string(19) "Request_Client_Curl"

protected Cache $_cache

Caching library for request caching

protected array $_options

curl options

Methods

public _send_message( Request $request ) (defined in Kohana_Request_Client_Stream)

Sends the HTTP message Request to a remote server and processes the response.

Parameters

  • Request $request required - Request to send

Tags

  • Uses - [PHP cURL](http://php.net/manual/en/book.curl.php)

Return Values

  • Response

Source Code

public function _send_message(Request $request)
{
    // Calculate stream mode
    $mode = ($request->method() === HTTP_Request::GET) ? 'r' : 'r+';
 
    // Process cookies
    if ($cookies = $request->cookie())
    {
        $request->headers('cookie', http_build_query($cookies, NULL, '; '));
    }
 
    // Get the message body
    $body = $request->body();
 
    if (is_resource($body))
    {
        $body = stream_get_contents($body);
    }
 
    // Set the content length
    $request->headers('content-length', (string) strlen($body));
 
    list($protocol) = explode('/', $request->protocol());
 
    // Create the context
    $options = array(
        strtolower($protocol) => array(
            'method'     => $request->method(),
            'header'     => (string) $request->headers(),
            'content'    => $body
        )
    );
 
    // Create the context stream
    $context = stream_context_create($options);
 
    stream_context_set_option($context, $this->_options);
 
    $uri = $request->uri();
 
    if ($query = $request->query())
    {
        $uri .= '?'.http_build_query($query, NULL, '&');
    }
 
    $stream = fopen($uri, $mode, FALSE, $context);
 
    $meta_data = stream_get_meta_data($stream);
 
    // Get the HTTP response code
    $http_response = array_shift($meta_data['wrapper_data']);
 
    if (preg_match_all('/(\w+\/\d\.\d) (\d{3})/', $http_response, $matches) !== FALSE)
    {
        $protocol = $matches[1][0];
        $status   = (int) $matches[2][0];
    }
    else
    {
        $protocol = NULL;
        $status   = NULL;
    }
 
    // Create a response
    $response = $request->create_response();
    $response_header = $response->headers();
 
    // Process headers
    array_map(array($response_header, 'parse_header_string'), array(), $meta_data['wrapper_data']);
 
    $response->status($status)
        ->protocol($protocol)
        ->body(stream_get_contents($stream));
 
    // Close the stream after use
    fclose($stream);
 
    return $response;
}

public execute_request( Request $request ) (defined in Kohana_Request_Client_External)

Processes the request, executing the controller action that handles this request, determined by the Route.

  1. Before the controller action is called, the Controller::before method will be called.
  2. Next the controller action will be called.
  3. After the controller action is called, the Controller::after method will be called.

By default, the output from the controller is captured and returned, and no headers are sent.

$request->execute();

Parameters

  • Request $request required - A request object

Tags

Return Values

  • Response

Source Code

public function execute_request(Request $request)
{
    if (Kohana::$profiling)
    {
        // Set the benchmark name
        $benchmark = '"'.$request->uri().'"';
 
        if ($request !== Request::$initial AND Request::$current)
        {
            // Add the parent request uri
            $benchmark .= ' « "'.Request::$current->uri().'"';
        }
 
        // Start benchmarking
        $benchmark = Profiler::start('Requests', $benchmark);
    }
 
    // Store the current active request and replace current with new request
    $previous = Request::$current;
    Request::$current = $request;
 
    // Resolve the POST fields
    if ($post = $request->post())
    {
        $request->body(http_build_query($post, NULL, '&'))
            ->headers('content-type', 'application/x-www-form-urlencoded');
    }
 
    // If Kohana expose, set the user-agent
    if (Kohana::$expose)
    {
        $request->headers('user-agent', 'Kohana Framework '.Kohana::VERSION.' ('.Kohana::CODENAME.')');
    }
 
    try
    {
        $response = $this->_send_message($request);
    }
    catch (Exception $e)
    {
        // Restore the previous request
        Request::$current = $previous;
 
        if (isset($benchmark))
        {
            // Delete the benchmark, it is invalid
            Profiler::delete($benchmark);
        }
 
        // Re-throw the exception
        throw $e;
    }
 
    // Restore the previous request
    Request::$current = $previous;
 
    if (isset($benchmark))
    {
        // Stop the benchmark
        Profiler::stop($benchmark);
    }
 
    // Return the response
    return $response;
}

public static factory( [ array $params = array(0) , string $client = NULL ] ) (defined in Kohana_Request_Client_External)

Factory method to create a new Request_Client_External object based on the client name passed, or defaulting to Request_Client_External::$client by default.

Request_Client_External::$client can be set in the application bootstrap.

Parameters

  • array $params = array(0) - Parameters to pass to the client
  • string $client = NULL - External client to use

Tags

Return Values

  • Request_Client_External

Source Code

public static function factory(array $params = array(), $client = NULL)
{
    if ($client === NULL)
    {
        $client = Request_Client_External::$client;
    }
 
    $client = new $client($params);
 
    if ( ! $client instanceof Request_Client_External)
    {
        throw new Request_Exception('Selected client is not a Request_Client_External object.');
    }
 
    return $client;
}

public options( [ mixed $key = NULL , mixed $value = NULL ] ) (defined in Kohana_Request_Client_External)

Set and get options for this request.

Parameters

  • mixed $key = NULL - Option name, or array of options
  • mixed $value = NULL - Option value

Return Values

  • mixed
  • Request_Client_External

Source Code

public function options($key = NULL, $value = NULL)
{
    if ($key === NULL)
        return $this->_options;
 
    if (is_array($key))
    {
        $this->_options = $key;
    }
    elseif ($value === NULL)
    {
        return Arr::get($this->_options, $key);
    }
    else
    {
        $this->_options[$key] = $value;
    }
 
    return $this;
}

public __construct( [ array $params = array(0) ] ) (defined in Kohana_Request_Client)

Creates a new Request_Client object, allows for dependency injection.

Parameters

  • array $params = array(0) - Params

Source Code

public function __construct(array $params = array())
{
    foreach ($params as $key => $value)
    {
        if (method_exists($this, $key))
        {
            $this->$key($value);
        }
    }
}

public cache( [ HTTP_Cache $cache = NULL ] ) (defined in Kohana_Request_Client)

Getter and setter for the internal caching engine, used to cache responses if available and valid.

Parameters

  • HTTP_Cache $cache = NULL - Engine to use for caching

Return Values

  • HTTP_Cache
  • Request_Client

Source Code

public function cache(HTTP_Cache $cache = NULL)
{
    if ($cache === NULL)
        return $this->_cache;
 
    $this->_cache = $cache;
    return $this;
}

public execute( Request $request ) (defined in Kohana_Request_Client)

Processes the request, executing the controller action that handles this request, determined by the Route.

  1. Before the controller action is called, the Controller::before method will be called.
  2. Next the controller action will be called.
  3. After the controller action is called, the Controller::after method will be called.

By default, the output from the controller is captured and returned, and no headers are sent.

$request->execute();

Parameters

  • Request $request required - $request

Tags

Return Values

  • Response

Source Code

public function execute(Request $request)
{
    if ($this->_cache instanceof HTTP_Cache)
        return $this->_cache->execute($this, $request);
 
    return $this->execute_request($request);
}