New in Symfony 4.4: HttpClient Improvements
November 18, 2019 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
The Symfony HttpClient component was introduced in Symfony 4.3 as a low-level HTTP client with support for both PHP stream wrappers and cURL. In Symfony 4.4 we've improved it with lots of new features.
In PR #31641 we made
$response->getInfo('debug')
to return extended logs about the HTTP
transaction so you can debug problems more easily.
This is an example of the generated log:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
* Found bundle for host http2-push.io: 0x56193881ae40 [can multiplex]\n
* Connected to http2-push.io (216.239.38.21) port 443 (#0)\n
> GET /css/style.css HTTP/2\r\n
Host: http2-push.io\r\n
User-Agent: Symfony HttpClient/Curl\r\n
Accept-Encoding: deflate, gzip\r\n
\r\n
In PR #31831 we added a new
cancel()
method so you can cancel responses at any moment.
In PR #31976 and PR #32104 we improved the integration of the component with HTTPlug, another popular library to make HTTP requests. The Symfony HttpClient component is now interoperable with three different abstractions for HTTP clients: Symfony Contracts, PSR-18 and HTTPlug v1 and v2.
In PR #32231 we added support for NTLM authentication:
1 2 3 4
$client = HttpClient::create([
'auth_ntlm' => 'username:password',
// ...
]);
In PR #32290 we added a new
toStream()
method to cast responses to regular PHP streams:
1 2 3 4 5 6 7 8 9 10
$response = $client->request('GET', 'https://...');
$content = $response->toStream();
// alternatively, you can use this code:
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Response\StreamWrapper;
$client = HttpClient::create();
$response = $client->request('GET', 'https://...');
$streamResource = StreamWrapper::createResource($response, $client);
Contributed by
Ruben Jacobs
in #32565.
In PR #32565 we improved the
buffer
configuration option. In previous Symfony versions this was a boolean
option. In Symfony 4.4 you can also pass a PHP closure (which must return a
boolean value) to enable buffering conditionally:
1 2 3 4 5 6 7
$response = $client->request('GET', 'https://...', [
'buffer' => function (array $headers): bool {
// some logic here to decide if we want to buffer or not
return true; // or return false;
},
]);
Contributed by
Peter Schultz
in #34216.
In PR #34216 we improved the
json
configuration option. In previous Symfony versions, this option only
allowed passing arrays or objects implementing JsonSerializable
. In Symfony
4.4 you can pass any value which can be passed to json_encode()
, which
includes scalar values and classes with public properties:
1 2 3 4 5 6 7 8 9 10
class Book
{
public $title = '...';
public $author = '...';
// ...
}
$response = $client->request('PUT', 'https://...', [
'json' => $book,
]);
Contributed by
Thomas Calvet
in #32807.
In PR #32807 we added a new
configuration option called max_duration
to define the maximum execution
time allowed for the whole request + response process. Set it to 0
to not
define any time limit:
1 2 3
$response = $client->request('GET', 'https://...', [
'max_duration' => 21.7,
]);
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.