lithium\net\http\Request::to()
Overrides
lithium\net\Message::to()
Converts the data in the record set to a different format, i.e. an array. Available options: array, URL, stream context configuration, or string.
Parameters
-
string
$formatFormat to convert to. Should be either
'url', which returns a string representation of the URL that this request points to, or'context', which returns an array usable with PHP'sstream_context_create()function. For more available formats, see the parent method,lithium\net\Message::to(). -
array
$optionsAllows overriding of specific portions of the URL, as follows. These options should only be specified if you intend to replace the values that are already in the
Requestobject.'scheme'string: The protocol scheme of the URL.'method'string: If applicable, the HTTP method to use in the request. Mainly applies to the'context'format.'host'string: The host name the request is pointing at.'port'string: The host port, if any.'path'string: The URL path.'query'mixed: The query string of the URL as a string or array.'auth'string: Authentication information. See the constructor for details.'content'string: The body of the request.'headers'array: The request headers.'version'string: The HTTP version of the request, where applicable.
Returns
mixedVaries; see the $format parameter for possible return values.
Source
public function to($format, array $options = []) {
$defaults = [
'method' => $this->method,
'scheme' => $this->scheme,
'host' => $this->host,
'port' => $this->port,
'path' => $this->path,
'query' => null,
'auth' => $this->auth,
'username' => $this->username,
'password' => $this->password,
'headers' => [],
'cookies' => [],
'proxy' => $this->_config['proxy'],
'body' => null,
'version' => $this->version,
'ignore_errors' => $this->_config['ignoreErrors'],
'follow_location' => $this->_config['followLocation'],
'request_fulluri' => (boolean) $this->_config['proxy']
];
$options += $defaults;
if (is_string($options['query'])) {
$options['query'] = "?" . $options['query'];
} elseif ($options['query']) {
$options['query'] = "?" . http_build_query($options['query']);
} elseif ($options['query'] === null) {
$options['query'] = $this->queryString();
}
if ($options['auth']) {
$data = [];
if (is_array($options['auth']) && !empty($options['auth']['nonce'])) {
$data = ['method' => $options['method'], 'uri' => $options['path']];
$data += $options['auth'];
}
$auth = $this->_classes['auth'];
$data = $auth::encode($options['username'], $options['password'], $data);
$this->headers('Authorization', $auth::header($data));
}
if ($this->cookies($options['cookies'])) {
$this->headers('Cookie', $this->_cookies());
}
$body = $this->body($options['body']);
if ($body || !in_array($options['method'], ['GET', 'HEAD', 'DELETE'])) {
$this->headers('Content-Length', strlen($body));
}
$conv = isset($this->_formats[$format]) ? $this->_formats[$format] : null;
return $conv ? $conv($this, $options, $defaults) : parent::to($format, $options);
}