Request_Client_External HTTP driver performs external requests using the php-http extention. 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_HTTP';
Class declared in SYSPATH/classes/kohana/request/client/http.php on line 19.
string
$clientdefines the external client to use by default
string(19) "Request_Client_Curl"
Cache
$_cacheCaching library for request caching
array
$_optionscurl options
Creates a new Request_Client
object,
allows for dependency injection.
array
$params
= array(0) - Paramspublic function __construct(array $params = array())
{
// Check that PECL HTTP supports requests
if ( ! http_support(HTTP_SUPPORT_REQUESTS))
{
throw new Request_Exception('Need HTTP request support!');
}
// Carry on
parent::__construct($params);
}
Sends the HTTP message Request to a remote server and processes the response.
Request
$request
required - Request to sendResponse
public function _send_message(Request $request)
{
$http_method_mapping = array(
HTTP_Request::GET => HTTPRequest::METH_GET,
HTTP_Request::HEAD => HTTPRequest::METH_HEAD,
HTTP_Request::POST => HTTPRequest::METH_POST,
HTTP_Request::PUT => HTTPRequest::METH_PUT,
HTTP_Request::DELETE => HTTPRequest::METH_DELETE,
HTTP_Request::OPTIONS => HTTPRequest::METH_OPTIONS,
HTTP_Request::TRACE => HTTPRequest::METH_TRACE,
HTTP_Request::CONNECT => HTTPRequest::METH_CONNECT,
);
// Create an http request object
$http_request = new HTTPRequest($request->uri(), $http_method_mapping[$request->method()]);
if ($this->_options)
{
// Set custom options
$http_request->setOptions($this->_options);
}
// Set headers
$http_request->setHeaders($request->headers()->getArrayCopy());
// Set cookies
$http_request->setCookies($request->cookie());
// Set query data (?foo=bar&bar=foo)
$http_request->setQueryData($request->query());
// Set the body
if ($request->method() == HTTP_Request::PUT)
{
$http_request->addPutData($request->body());
}
else
{
$http_request->setBody($request->body());
}
try
{
$http_request->send();
}
catch (HTTPRequestException $e)
{
throw new Request_Exception($e->getMessage());
}
catch (HTTPMalformedHeaderException $e)
{
throw new Request_Exception($e->getMessage());
}
catch (HTTPEncodingException $e)
{
throw new Request_Exception($e->getMessage());
}
// Create the response
$response = $request->create_response();
// Build the response
$response->status($http_request->getResponseCode())
->headers($http_request->getResponseHeader())
->cookie($http_request->getResponseCookies())
->body($http_request->getResponseBody());
return $response;
}
Processes the request, executing the controller action that handles this request, determined by the Route.
By default, the output from the controller is captured and returned, and no headers are sent.
$request->execute();
Request
$request
required - A request objectResponse
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;
}
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.
array
$params
= array(0) - Parameters to pass to the clientstring
$client
= NULL - External client to useRequest_Client_External
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;
}
Set and get options for this request.
mixed
$key
= NULL - Option name, or array of optionsmixed
$value
= NULL - Option valuemixed
Request_Client_External
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;
}
Getter and setter for the internal caching engine, used to cache responses if available and valid.
HTTP_Cache
$cache
= NULL - Engine to use for cachingHTTP_Cache
Request_Client
public function cache(HTTP_Cache $cache = NULL)
{
if ($cache === NULL)
return $this->_cache;
$this->_cache = $cache;
return $this;
}
Processes the request, executing the controller action that handles this request, determined by the Route.
By default, the output from the controller is captured and returned, and no headers are sent.
$request->execute();
Request
$request
required - $requestResponse
public function execute(Request $request)
{
if ($this->_cache instanceof HTTP_Cache)
return $this->_cache->execute($this, $request);
return $this->execute_request($request);
}