You are here

class HttpClient in Http Client 7.2

Same name and namespace in other branches
  1. 6.2 includes/HttpClient.inc \HttpClient

A http client.

Hierarchy

Expanded class hierarchy of HttpClient

1 string reference to 'HttpClient'
HttpClient::urlencode_rfc3986 in includes/HttpClient.inc
Stolen from OAuth_common

File

includes/HttpClient.inc, line 6

View source
class HttpClient {
  protected $authentication = NULL;
  protected $request_alter = NULL;
  protected $formatter = NULL;
  protected $lastError = FALSE;
  protected $delegate = NULL;
  public $lastRequest;
  public $rawResponse;
  public $lastResponse;

  /**
   * Allows specification of additional custom options.
   */
  public $options = array();

  /**
   * Creates a Http client.
   *
   * @param HttpClientAuthentication $authentication
   *  Optional. Authentication to use for the request.
   * @param HttpClientFormatter $formatter
   *  Optional. Formatter to use for request and response bodies.
   * @param mixed $request_alter
   *  Optional. Either a callable, a object with a public alterRequest method,
   *  a class that has a public static alterRequestMethod or FALSE.
   * @param HttpClientDelegate $delegate
   *  Optional. The delegate that executes the call for the HttpClient.
   *  Defaults to a HttpClientCurlDelegate if curl is available.
   */
  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;
  }

  /**
   * Inject authentication class
   *
   * @param HttpClientAuthentication $auth
   *   The class to use for authentication.
   */
  public function setAuthentication(HttpClientAuthentication $auth) {
    $this->authentication = $auth;
  }

  /**
   * Inject formatter class
   *
   * @param HttpClientFormatter $formatter
   *   The class to use for formatting.
   */
  public function setFormatter(HttpClientFormatter $formatter) {
    $this->formatter = $formatter;
  }

  /**
   * Executes a GET request.
   */
  public function get($url, $parameters = array()) {
    return $this
      ->execute(new HttpClientRequest($url, array(
      'method' => 'GET',
      'parameters' => $parameters,
    )));
  }

  /**
   * Executes a POST request.
   */
  public function post($url, $data = NULL, $parameters = array()) {
    return $this
      ->execute(new HttpClientRequest($url, array(
      'method' => 'POST',
      'parameters' => $parameters,
      'data' => $data,
    )));
  }

  /**
   * Executes a PUT request.
   */
  public function put($url, $data = NULL, $parameters = array()) {
    return $this
      ->execute(new HttpClientRequest($url, array(
      'method' => 'PUT',
      'parameters' => $parameters,
      'data' => $data,
    )));
  }

  /**
   * Executes a DELETE request.
   */
  public function delete($url, $parameters = array()) {
    return $this
      ->execute(new HttpClientRequest($url, array(
      'method' => 'DELETE',
      'parameters' => $parameters,
    )));
  }

  /**
   * Executes the given request.
   */
  public function execute(HttpClientRequest $request) {

    // Allow the request to be altered
    if ($this->request_alter) {
      call_user_func($this->request_alter, $request);
    }
    if (isset($request->data)) {
      if ($this->formatter) {
        $request
          ->setHeader('Content-type', $this->formatter
          ->contentType());
        $request->data = $this->formatter
          ->serialize($request->data);
      }
      else {
        $request->data = (string) $request->data;
      }
      if (is_string($request->data)) {
        $request
          ->setHeader('Content-length', strlen($request->data));
      }
    }
    if ($this->formatter) {
      $request
        ->setHeader('Accept', $this->formatter
        ->accepts());
    }

    // Allow the authentication implementation to do it's magic
    if ($this->authentication) {
      $this->authentication
        ->authenticate($request);
    }
    $response = $this->delegate
      ->execute($this, $request);
    $this->lastRequest = $request;
    $this->lastResponse = $response;
    $result = NULL;
    if ($response->responseCode >= 200 && $response->responseCode <= 299) {
      if ($this->formatter) {
        try {
          $result = $this->formatter
            ->unserialize($response->body);
        } catch (Exception $e) {
          throw new HttpClientException('Failed to unserialize response', 0, $response, $e);
        }
      }
      else {
        $result = $response->body;
      }
    }
    elseif (!empty($response->drupalErrors)) {
      throw new HttpClientException(check_plain(implode("\n", $response->drupalErrors)), $response->responseCode, $response);
    }
    else {
      throw new HttpClientException(check_plain($response->responseMessage), $response->responseCode, $response);
    }
    return $result;
  }

  /**
   * Stolen from OAuth_common
   */
  public static function urlencode_rfc3986($input) {
    if (is_array($input)) {
      return array_map(array(
        'HttpClient',
        'urlencode_rfc3986',
      ), $input);
    }
    else {
      if (is_scalar($input)) {
        return str_replace('+', ' ', str_replace('%7E', '~', rawurlencode($input)));
      }
      else {
        return '';
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HttpClient::$authentication protected property
HttpClient::$delegate protected property
HttpClient::$formatter protected property
HttpClient::$lastError protected property
HttpClient::$lastRequest public property
HttpClient::$lastResponse public property
HttpClient::$options public property Allows specification of additional custom options.
HttpClient::$rawResponse public property
HttpClient::$request_alter protected property
HttpClient::delete public function Executes a DELETE request.
HttpClient::execute public function Executes the given request.
HttpClient::get public function Executes a GET request.
HttpClient::post public function Executes a POST request.
HttpClient::put public function Executes a PUT request.
HttpClient::setAuthentication public function Inject authentication class
HttpClient::setFormatter public function Inject formatter class
HttpClient::urlencode_rfc3986 public static function Stolen from OAuth_common
HttpClient::__construct public function Creates a Http client.