You are here

public function HttpClient::__construct in Http Client 7.2

Same name and namespace in other branches
  1. 6.2 includes/HttpClient.inc \HttpClient::__construct()

Creates a Http client.

Parameters

HttpClientAuthentication $authentication: Optional. Authentication to use for the request.

HttpClientFormatter $formatter: Optional. Formatter to use for request and response bodies.

mixed $request_alter: Optional. Either a callable, a object with a public alterRequest method, a class that has a public static alterRequestMethod or FALSE.

HttpClientDelegate $delegate: Optional. The delegate that executes the call for the HttpClient. Defaults to a HttpClientCurlDelegate if curl is available.

File

includes/HttpClient.inc, line 38

Class

HttpClient
A http client.

Code

public function __construct($authentication = NULL, $formatter = NULL, $request_alter = FALSE, $delegate = NULL) {
  $this->authentication = $authentication;
  $this->formatter = $formatter;
  if (!$formatter || in_array('HttpClientFormatter', class_implements($formatter))) {
    $this->formatter = $formatter;
  }
  else {
    throw new Exception(t('The formatter parameter must either be a object implementing HttpClientFormatter, or evaluate to FALSE.'));
  }
  if (is_object($request_alter) && is_callable(array(
    $request_alter,
    'alterRequest',
  ))) {
    $request_alter = array(
      $request_alter,
      'alterRequest',
    );
  }
  if (!$request_alter || is_callable($request_alter)) {
    $this->request_alter = $request_alter;
  }
  else {
    throw new Exception(t('The request_alter parameter must either be a object or class with a public alterRequest method, callable in itself or evaluate to FALSE.'));
  }
  if (!$delegate && function_exists('curl_init')) {
    $delegate = new HttpClientCurlDelegate();
  }
  if (!$delegate) {
    throw new Exception(t('The HttpClient cannot execute requests without a delegate. This probably means that you don\'t have curl installed on your system.'));
  }
  $this->delegate = $delegate;
}