public function to($format, array $options = array())
{
$defaults = array('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' => array(), 'cookies' => array(), 'proxy' => $this->_config['proxy'], 'body' => null, 'version' => $this->version, 'ignore_errors' => $this->_config['ignoreErrors'], 'follow_location' => $this->_config['followLocation'], 'request_fulluri' => (bool) $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 = array();
if (is_array($options['auth']) && !empty($options['auth']['nonce'])) {
$data = array('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'], array('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);
}