2
namespace GuzzleHttp\Message;
4
use GuzzleHttp\Collection;
5
use GuzzleHttp\Event\HasEmitterTrait;
6
use GuzzleHttp\Subscriber\Prepare;
10
* HTTP request class to send requests
12
class Request extends AbstractMessage implements RequestInterface
16
/** @var Url HTTP Url */
19
/** @var string HTTP method */
22
/** @var Collection Transfer options */
23
private $transferOptions;
26
* @param string $method HTTP method
27
* @param string|Url $url HTTP URL to connect to. The URI scheme,
28
* host header, and URI are parsed from the full URL. If query string
29
* parameters are present they will be parsed as well.
30
* @param array|Collection $headers HTTP headers
31
* @param mixed $body Body to send with the request
32
* @param array $options Array of options to use with the request
33
* - emitter: Event emitter to use with the request
35
public function __construct(
43
$this->method = strtoupper($method);
44
$this->handleOptions($options);
45
$this->transferOptions = new Collection($options);
46
$this->addPrepareEvent();
49
$this->setBody($body);
53
foreach ($headers as $key => $value) {
54
$this->setHeader($key, $value);
59
public function __clone()
62
$this->emitter = clone $this->emitter;
64
$this->transferOptions = clone $this->transferOptions;
65
$this->url = clone $this->url;
68
public function setUrl($url)
70
$this->url = $url instanceof Url ? $url : Url::fromString($url);
71
$this->updateHostHeaderFromUrl();
76
public function getUrl()
78
return (string) $this->url;
81
public function setQuery($query)
83
$this->url->setQuery($query);
88
public function getQuery()
90
return $this->url->getQuery();
93
public function setMethod($method)
95
$this->method = strtoupper($method);
100
public function getMethod()
102
return $this->method;
105
public function getScheme()
107
return $this->url->getScheme();
110
public function setScheme($scheme)
112
$this->url->setScheme($scheme);
117
public function getPort()
119
return $this->url->getPort();
122
public function setPort($port)
124
$this->url->setPort($port);
125
$this->updateHostHeaderFromUrl();
130
public function getHost()
132
return $this->url->getHost();
135
public function setHost($host)
137
$this->url->setHost($host);
138
$this->updateHostHeaderFromUrl();
143
public function getPath()
145
return '/' . ltrim($this->url->getPath(), '/');
148
public function setPath($path)
150
$this->url->setPath($path);
155
public function getResource()
157
$resource = $this->getPath();
158
if ($query = (string) $this->url->getQuery()) {
159
$resource .= '?' . $query;
165
public function getConfig()
167
return $this->transferOptions;
170
protected function handleOptions(array &$options)
172
parent::handleOptions($options);
173
// Use a custom emitter if one is specified, and remove it from
174
// options that are exposed through getConfig()
175
if (isset($options['emitter'])) {
176
$this->emitter = $options['emitter'];
177
unset($options['emitter']);
182
* Adds a subscriber that ensures a request's body is prepared before
185
private function addPrepareEvent()
189
$subscriber = new Prepare();
192
$this->getEmitter()->attach($subscriber);
195
private function updateHostHeaderFromUrl()
197
$port = $this->url->getPort();
198
$scheme = $this->url->getScheme();
199
if ($host = $this->url->getHost()) {
200
if (($port == 80 && $scheme == 'http') ||
201
($port == 443 && $scheme == 'https')
203
$this->setHeader('Host', $host);
205
$this->setHeader('Host', "{$host}:{$port}");