Symfony News

New in Symfony 4.3: HttpClient component

Nicolas Grekas

Contributed by
Nicolas Grekas
in #30413.

Making HTTP requests (e.g. to third-party APIs) is a frequent need for developers working on web applications. In Symfony 4.3 we'll make this simpler with a brand new component called HttpClient.

Basic usage

The Symfony\Component\HttpClient\HttpClient class provided to make HTTP requests is quite straightforward:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
use Symfony\Component\HttpClient\HttpClient;

$httpClient = HttpClient::create();
$response = $httpClient->request('GET', 'https://api.github.com/repos/symfony/symfony-docs');

$statusCode = $response->getStatusCode();
// $statusCode = 200
$content = $response->getContent();
// returns the raw content returned by the server (JSON in this case)
// $content = '{"id":521583, "name":"symfony-docs", ...}'
$content = $response->toArray();
// transforms the response JSON content into a PHP array
// $content = ['id' => 521583, 'name' => 'symfony-docs', ...]

By default, the component uses native PHP functions to make the HTTP requests, so you don't have to install any other dependency. However, it will use the cURL based transport if your system has both the cURL library and the PHP cURL extension installed.

When the HTTP status code of the response is not in the 200-299 range (i.e. 3xx, 4xx or 5xx) your code is expected to handle it. If you don't do that, the getHeaders() and getContent() methods throw an appropriate exception:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// the response of this request will be a 403 HTTP error
$response = $httpClient->request('GET', 'https://httpbin.org/status/403');

// this code results in a Symfony\Component\HttpClient\Exception\ClientException
// because it doesn't check the status code of the response
$content = $response->getContent();

// do this instead
if (200 !== $response->getStatusCode()) {
    // handle the HTTP request error (e.g. retry the request)
} else {
    $content = $response->getContent();
}

Features

The new HttpClient component is packed with useful features. All of them are explained in the docs:

Symfony framework integration

When using HttpClient inside a full Symfony application instead of as a stand-alone component, you can configure it under the http_client key (check out the full HttpClient configuration reference):

1
2
3
4
5
6
# config/packages/framework.yaml
framework:
    # ...
    http_client:
        max_redirects: 7
        max_host_connections: 10

Then, you can inject the HttpClient in other services as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
use Symfony\Contracts\HttpClient\HttpClientInterface;

class SomeService
{
    private $httpClient;

    public function __construct(HttpClientInterface $httpClient)
    {
        $this->httpClient = $httpClient;
    }
}

Future integrations

Having a default and official HTTP client for Symfony applications will allow us to implement other features that require communicating with third-party services. A recent example of this is the NotCompromisedPassword validator, which makes your applications more secure and uses the HttpClient component to make the HTTP requests required to check if a given password was publicly compromised or not.


Be trained by Symfony experts - 2019-05-15 Clichy - 2019-05-20 Lille - 2019-05-20 Clichy


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook