Create a new request based on the HTTP method
string $method HTTP method (GET, POST, PUT, PATCH, HEAD, DELETE, ...):
string|Url $url HTTP URL to connect to:
array|Collection $headers HTTP headers:
string|resource|array|EntityBodyInterface $body Body to send in the request:
Overrides RequestFactoryInterface::create
public function create($method, $url, $headers = null, $body = null) {
$method = strtoupper($method);
if ($method == 'GET' || $method == 'HEAD' || $method == 'TRACE' || $method == 'OPTIONS') {
$c = $this->requestClass;
$request = new $c($method, $url, $headers);
if ($body) {
// The body is where the response body will be stored
$request
->setResponseBody(EntityBody::factory($body));
}
return $request;
}
$c = $this->entityEnclosingRequestClass;
$request = new $c($method, $url, $headers);
if ($body) {
$isChunked = (string) $request
->getHeader('Transfer-Encoding') == 'chunked';
if ($method == 'POST' && (is_array($body) || $body instanceof Collection)) {
// Normalize PHP style cURL uploads with a leading '@' symbol
$files = array();
foreach ($body as $key => $value) {
if (is_string($value) && strpos($value, '@') === 0) {
$files[$key] = $value;
unset($body[$key]);
}
}
// Add the fields if they are still present and not all files
if (count($body) > 0) {
$request
->addPostFields($body);
}
// Add any files that were prefixed with '@'
if (!empty($files)) {
$request
->addPostFiles($files);
}
if ($isChunked) {
$request
->setHeader('Transfer-Encoding', 'chunked');
}
}
elseif (is_resource($body) || $body instanceof EntityBody) {
$request
->setBody($body, (string) $request
->getHeader('Content-Type'), $isChunked);
}
else {
$request
->setBody((string) $body, (string) $request
->getHeader('Content-Type'), $isChunked);
}
}
return $request;
}