lithium\net\http\Request::__construct PHP Method

__construct() public method

Constructor. Adds config values to the public properties when a new object is created.
See also: lithium\net\http\Message::__construct()
See also: lithium\net\Message::__construct()
public __construct ( array $config = [] ) : void
$config array The available configuration options are the following. Further options are inherited from the parent classes. - `'method'` _string_: Defaults to `'GET'`. - `'path'` _string_: Defaults to `null`. - `'query'` _array_: Defaults to `array()`. - `'cookies'` _array_: Defaults to `array()`. - `'type'` _string_: Defaults to `null`. - `'auth'` _mixed_: Defaults to `null`. - `'proxy'` _string_: Defaults to `null`. - `'ignoreErrors'` _boolean_: Defaults to `true`. - `'followLocation'` _boolean_: Defaults to `true`.
return void
    public function __construct(array $config = array())
    {
        $defaults = array('method' => 'GET', 'query' => array(), 'cookies' => array(), 'type' => null, 'auth' => null, 'proxy' => null, 'ignoreErrors' => true, 'followLocation' => true);
        $config += $defaults;
        $this->method = $config['method'];
        $this->query = $config['query'];
        $this->auth = $config['auth'];
        parent::__construct($config);
        $this->headers = array('Host' => $this->port ? "{$this->host}:{$this->port}" : $this->host, 'Connection' => 'Close', 'User-Agent' => 'Mozilla/5.0');
        foreach (array('type', 'headers', 'cookies') as $field) {
            if ($value = $this->_config[$field]) {
                $this->{$field}($value);
            }
        }
        if ($cookies = $this->headers('Cookie')) {
            $this->_parseCookies($cookies);
        }
        $this->_formats += array('url' => function ($req, $options) {
            $options['port'] = $options['port'] ? ":{$options['port']}" : '';
            $options['path'] = str_replace('//', '/', $options['path']);
            return String::insert("{:scheme}://{:host}{:port}{:path}{:query}", $options);
        }, 'context' => function ($req, $options, $defaults) {
            $req->headers($options['headers']);
            return array('http' => array_diff_key($options, $defaults) + array('content' => $req->body(), 'method' => $options['method'], 'header' => $req->headers(), 'protocol_version' => $options['version'], 'ignore_errors' => $options['ignore_errors'], 'follow_location' => $options['follow_location'], 'request_fulluri' => $options['request_fulluri'], 'proxy' => $options['proxy']));
        }, 'string' => function ($req, $options) {
            $body = $req->body();
            $path = str_replace('//', '/', $options['path']) . $options['query'];
            $status = "{$options['method']} {$path} {$req->protocol}";
            return join("\r\n", array($status, join("\r\n", $req->headers()), "", $body));
        });
    }

Usage Example

Example #1
0
 /**
  * Adds config values to the public properties when a new object is created, pulling
  * request data from superglobals if `globals` is set to `true`.
  *
  * @param array $config Configuration options : default values are:
  *        - `'base'` _string_: null
  *        - `'url'` _string_: null
  *        - `'protocol'` _string_: null
  *        - `'version'` _string_: '1.1'
  *        - `'method'` _string_: 'GET'
  *        - `'scheme'` _string_: 'http'
  *        - `'host'` _string_: 'localhost'
  *        - `'port'` _integer_: null
  *        - `'username'` _string_: null
  *        - `'password'` _string_: null
  *        - `'path'` _string_: null
  *        - `'query'` _array_: array()
  *        - `'headers'` _array_: array()
  *        - `'type'` _string_: null
  *        - `'auth'` _mixed_: null
  *        - `'body'` _mixed_: null
  *        - `'data'` _array_: array()
  *        - `'env'` _array_: array()
  *        - `'globals'` _boolean_: true
  */
 public function __construct(array $config = array())
 {
     $defaults = array('base' => null, 'url' => null, 'env' => array(), 'query' => array(), 'data' => array(), 'globals' => true);
     $config += $defaults;
     if ($config['globals'] === true) {
         if (isset($_SERVER)) {
             $config['env'] += $_SERVER;
         }
         if (isset($_ENV)) {
             $config['env'] += $_ENV;
         }
         if (isset($_GET)) {
             $config['query'] += $_GET;
         }
         if (isset($_POST)) {
             $config['data'] += $_POST;
         }
     }
     $this->_env = $config['env'];
     if (!isset($config['host'])) {
         $config['host'] = $this->env('HTTP_HOST');
     }
     if (!isset($config['protocol'])) {
         $config['protocol'] = $this->env('SERVER_PROTOCOL');
     }
     if ($config['protocol'] && strpos($config['protocol'], '/')) {
         list($scheme, $version) = explode('/', $config['protocol']);
         $https = $this->env('HTTPS') ? 's' : '';
         $scheme = strtolower($scheme) . $https;
         if (!isset($config['scheme'])) {
             $config['scheme'] = $scheme;
         }
         if (!isset($config['version'])) {
             $config['version'] = $version;
         }
     }
     $this->_base = $this->_base($config['base']);
     $this->url = $this->_url($config['url']);
     parent::__construct($config);
     $this->headers('Content-Type', $this->env('CONTENT_TYPE'));
     $this->headers('Content-Length', $this->env('CONTENT_LENGTH'));
     foreach ($this->_env as $name => $value) {
         if (substr($name, 0, 5) == 'HTTP_') {
             $name = str_replace('_', ' ', substr($name, 5));
             $name = str_replace(' ', '-', ucwords(strtolower($name)));
             $this->headers($name, $value);
         }
     }
 }
All Usage Examples Of lithium\net\http\Request::__construct