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->addHeader($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();
74
public function getUrl()
76
return (string) $this->url;
79
public function setQuery($query)
81
$this->url->setQuery($query);
84
public function getQuery()
86
return $this->url->getQuery();
89
public function setMethod($method)
91
$this->method = strtoupper($method);
94
public function getMethod()
99
public function getScheme()
101
return $this->url->getScheme();
104
public function setScheme($scheme)
106
$this->url->setScheme($scheme);
109
public function getPort()
111
return $this->url->getPort();
114
public function setPort($port)
116
$this->url->setPort($port);
117
$this->updateHostHeaderFromUrl();
120
public function getHost()
122
return $this->url->getHost();
125
public function setHost($host)
127
$this->url->setHost($host);
128
$this->updateHostHeaderFromUrl();
131
public function getPath()
133
return '/' . ltrim($this->url->getPath(), '/');
136
public function setPath($path)
138
$this->url->setPath($path);
141
public function getResource()
143
$resource = $this->getPath();
144
if ($query = (string) $this->url->getQuery()) {
145
$resource .= '?' . $query;
151
public function getConfig()
153
return $this->transferOptions;
156
protected function handleOptions(array &$options)
158
parent::handleOptions($options);
159
// Use a custom emitter if one is specified, and remove it from
160
// options that are exposed through getConfig()
161
if (isset($options['emitter'])) {
162
$this->emitter = $options['emitter'];
163
unset($options['emitter']);
168
* Adds a subscriber that ensures a request's body is prepared before
171
private function addPrepareEvent()
175
$subscriber = new Prepare();
178
$this->getEmitter()->attach($subscriber);
181
private function updateHostHeaderFromUrl()
183
$port = $this->url->getPort();
184
$scheme = $this->url->getScheme();
185
if ($host = $this->url->getHost()) {
186
if (($port == 80 && $scheme == 'http') ||
187
($port == 443 && $scheme == 'https')
189
$this->setHeader('Host', $host);
191
$this->setHeader('Host', "{$host}:{$port}");