Create and return a new { Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client. The URI can contain the query string as well. Use an array to provide a URI template and additional variables to use in the URI template expansion.
string $method HTTP method. Defaults to GET:
string|array $uri Resource URI.:
array|Collection $headers HTTP headers:
string|resource|array|EntityBodyInterface $body Entity body of request (POST/PUT) or response (GET):
InvalidArgumentException if a URI array is passed that does not contain exactly two elements: the URI followed by template variables
Overrides ClientInterface::createRequest
RequestInterface} configured for the client.
public function createRequest($method = RequestInterface::GET, $uri = null, $headers = null, $body = null) {
if (!is_array($uri)) {
$templateVars = null;
}
else {
if (count($uri) != 2 || !isset($uri[1]) || !is_array($uri[1])) {
throw new InvalidArgumentException('You must provide a URI template followed by an array of template variables ' . 'when using an array for a URI template');
}
list($uri, $templateVars) = $uri;
}
if (!$uri) {
$url = $this
->getBaseUrl();
}
elseif (substr($uri, 0, 4) === 'http') {
// Use absolute URLs as-is
$url = $this
->expandTemplate($uri, $templateVars);
}
else {
$url = Url::factory($this
->getBaseUrl())
->combine($this
->expandTemplate($uri, $templateVars));
}
if ($this->userAgent) {
$this->defaultHeaders
->set('User-Agent', $this->userAgent);
}
// If default headers are provided, then merge them into existing headers
// If a collision occurs, the header is completely replaced
if (count($this->defaultHeaders)) {
if ($headers instanceof Collection) {
$headers = array_merge($this->defaultHeaders
->getAll(), $headers
->getAll());
}
elseif (is_array($headers)) {
$headers = array_merge($this->defaultHeaders
->getAll(), $headers);
}
else {
$headers = $this->defaultHeaders;
}
}
return $this
->prepareRequest($this->requestFactory
->create($method, (string) $url, $headers, $body));
}