You are here

class HttpClientRequest in Http Client 7.2

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

This is a convenience class that allows the manipulation of a http request before it's handed over to curl.

Hierarchy

Expanded class hierarchy of HttpClientRequest

File

includes/HttpClient.inc, line 532

View source
class HttpClientRequest {
  const METHOD_GET = 'GET';
  const METHOD_POST = 'POST';
  const METHOD_PUT = 'PUT';
  const METHOD_DELETE = 'DELETE';
  public $method = self::METHOD_GET;
  public $url = '';
  public $parameters = array();
  public $headers = array();
  public $data = NULL;

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

  /**
   * Construct a new client request.
   *
   * @param $url
   *   The url to send the request to.
   * @param $values
   *   An array of values for the object properties to set for the request.
   */
  public function __construct($url, $values = array()) {
    $this->url = $url;
    foreach (get_object_vars($this) as $key => $value) {
      if (isset($values[$key])) {
        $this->{$key} = $values[$key];
      }
    }
  }

  /**
   * Gets the values of a header, or the value of the header if
   * $treat_as_single is set to true.
   *
   * @param string $name
   * @param string $treat_as_single
   *  Optional. If set to FALSE an array of values will be returned. Otherwise
   *  The first value of the header will be returned.
   * @return string|array
   */
  public function getHeader($name, $treat_as_single = TRUE) {
    $value = NULL;
    if (!empty($this->headers[$name])) {
      if ($treat_as_single) {
        $value = reset($this->headers[$name]);
      }
      else {
        $value = $this->headers[$name];
      }
    }
    return $value;
  }

  /**
   * Returns the headers as a array. Multiple valued headers will have their
   * values concatenated and separated by a comma as per
   * http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
   *
   * @return array
   */
  public function getHeaders() {
    $headers = array();
    foreach ($this->headers as $name => $values) {
      $headers[] = $name . ': ' . join(', ', $values);
    }
    return $headers;
  }

  /**
   * Appends a header value. Use HttpClientRequest::setHeader() it you want to
   * set the value of a header.
   *
   * @param string $name
   * @param string $value
   * @return void
   */
  public function addHeader($name, $value) {
    if (!is_array($value)) {
      $this->headers[$name][] = $value;
    }
    else {
      $values = isset($this->headers[$name]) ? $this->headers[$name] : array();
      $this->headers[$name] = $values + $value;
    }
  }

  /**
   * Sets a header value.
   *
   * @param string $name
   * @param string $value
   * @return void
   */
  public function setHeader($name, $value) {
    if (!is_array($value)) {
      $this->headers[$name][] = $value;
    }
    else {
      $this->headers[$name] = $value;
    }
  }

  /**
   * Removes a header.
   *
   * @param string $name
   * @return void
   */
  public function removeHeader($name) {
    unset($this->headers[$name]);
  }

  /**
   * Returns the url taken the parameters into account.
   */
  public function url() {
    if (empty($this->parameters)) {
      return $this->url;
    }
    $total = array();
    foreach ($this->parameters as $k => $v) {
      if (is_array($v)) {
        foreach ($v as $va) {
          $total[] = HttpClient::urlencode_rfc3986($k) . "[]=" . HttpClient::urlencode_rfc3986($va);
        }
      }
      else {
        $total[] = HttpClient::urlencode_rfc3986($k) . "=" . HttpClient::urlencode_rfc3986($v);
      }
    }
    $out = implode("&", $total);
    return $this->url . '?' . $out;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HttpClientRequest::$data public property
HttpClientRequest::$headers public property
HttpClientRequest::$method public property
HttpClientRequest::$options public property Allows specification of additional custom options.
HttpClientRequest::$parameters public property
HttpClientRequest::$url public property
HttpClientRequest::addHeader public function Appends a header value. Use HttpClientRequest::setHeader() it you want to set the value of a header.
HttpClientRequest::getHeader public function Gets the values of a header, or the value of the header if $treat_as_single is set to true.
HttpClientRequest::getHeaders public function Returns the headers as a array. Multiple valued headers will have their values concatenated and separated by a comma as per http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
HttpClientRequest::METHOD_DELETE constant
HttpClientRequest::METHOD_GET constant
HttpClientRequest::METHOD_POST constant
HttpClientRequest::METHOD_PUT constant
HttpClientRequest::removeHeader public function Removes a header.
HttpClientRequest::setHeader public function Sets a header value.
HttpClientRequest::url public function Returns the url taken the parameters into account.
HttpClientRequest::__construct public function Construct a new client request.