Request_Client_External Curl driver performs external requests using the php-curl extention. This is the default driver for all external requests.
Class declared in SYSPATH/classes/kohana/request/client/curl.php on line 13.
string
$clientdefines the external client to use by default
string(19) "Request_Client_Curl"
Cache
$_cacheCaching library for request caching
array
$_optionscurl options
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)
{
// Response headers
$response_headers = array();
$options = array();
// Set the request method
$options = $this->_set_curl_request_method($request, $options);
// Set the request body. This is perfectly legal in CURL even
// if using a request other than POST. PUT does support this method
// and DOES NOT require writing data to disk before putting it, if
// reading the PHP docs you may have got that impression. SdF
$options[CURLOPT_POSTFIELDS] = $request->body();
// Process headers
if ($headers = $request->headers())
{
$http_headers = array();
foreach ($headers as $key => $value)
{
$http_headers[] = $key.': '.$value;
}
$options[CURLOPT_HTTPHEADER] = $http_headers;
}
// Process cookies
if ($cookies = $request->cookie())
{
$options[CURLOPT_COOKIE] = http_build_query($cookies, NULL, '; ');
}
// Create response
$response = $request->create_response();
$response_header = $response->headers();
// Implement the standard parsing parameters
$options[CURLOPT_HEADERFUNCTION] = array($response_header, 'parse_header_string');
$this->_options[CURLOPT_RETURNTRANSFER] = TRUE;
$this->_options[CURLOPT_HEADER] = FALSE;
// Apply any additional options set to
$options += $this->_options;
$uri = $request->uri();
if ($query = $request->query())
{
$uri .= '?'.http_build_query($query, NULL, '&');
}
// Open a new remote connection
$curl = curl_init($uri);
// Set connection options
if ( ! curl_setopt_array($curl, $options))
{
throw new Request_Exception('Failed to set CURL options, check CURL documentation: :url',
array(':url' => 'http://php.net/curl_setopt_array'));
}
// Get the response body
$body = curl_exec($curl);
// Get the response information
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($body === FALSE)
{
$error = curl_error($curl);
}
// Close the connection
curl_close($curl);
if (isset($error))
{
throw new Request_Exception('Error fetching remote :url [ status :code ] :error',
array(':url' => $request->url(), ':code' => $code, ':error' => $error));
}
$response->status($code)
->body($body);
return $response;
}
Sets the appropriate curl request options. Uses the responding options for POST and PUT, uses CURLOPT_CUSTOMREQUEST otherwise
Request
$request
required - $requestarray
$options
required - $optionsarray
public function _set_curl_request_method(Request $request, array $options)
{
switch ($request->method()) {
case Request::POST:
$options[CURLOPT_POST] = TRUE;
break;
case Request::PUT:
$options[CURLOPT_PUT] = TRUE;
break;
default:
$options[CURLOPT_CUSTOMREQUEST] = $request->method();
break;
}
return $options;
}
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;
}
Creates a new Request_Client
object,
allows for dependency injection.
array
$params
= array(0) - Paramspublic function __construct(array $params = array())
{
foreach ($params as $key => $value)
{
if (method_exists($this, $key))
{
$this->$key($value);
}
}
}
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);
}