HTTP Client

HTTP Client

New in version 4.3: The HttpClient component was introduced in Symfony 4.3.

Installation

The HttpClient component is a low-level HTTP client with support for both PHP stream wrappers and cURL. It provides utilities to consume APIs and supports synchronous and asynchronous operations. You can install it with:

  1. $ composer require symfony/http-client

Basic Usage

Use the Symfony\Component\HttpClient\HttpClient class to make requests. In the Symfony framework, this class is available as the http_client service. This service will be autowired automatically when type-hinting for Symfony\Contracts\HttpClient\HttpClientInterface:

  • Framework Use

    1. use Symfony\Contracts\HttpClient\HttpClientInterface;
    2. class SymfonyDocs
    3. {
    4. private $client;
    5. public function __construct(HttpClientInterface $client)
    6. {
    7. $this->client = $client;
    8. }
    9. public function fetchGitHubInformation(): array
    10. {
    11. $response = $this->client->request(
    12. 'GET',
    13. 'https://api.github.com/repos/symfony/symfony-docs'
    14. );
    15. $statusCode = $response->getStatusCode();
    16. // $statusCode = 200
    17. $contentType = $response->getHeaders()['content-type'][0];
    18. // $contentType = 'application/json'
    19. $content = $response->getContent();
    20. // $content = '{"id":521583, "name":"symfony-docs", ...}'
    21. $content = $response->toArray();
    22. // $content = ['id' => 521583, 'name' => 'symfony-docs', ...]
    23. return $content;
    24. }
    25. }
  • Standalone Use

    1. use Symfony\Component\HttpClient\HttpClient;
    2. $client = HttpClient::create();
    3. $response = $client->request('GET', 'https://api.github.com/repos/symfony/symfony-docs');
    4. $statusCode = $response->getStatusCode();
    5. // $statusCode = 200
    6. $contentType = $response->getHeaders()['content-type'][0];
    7. // $contentType = 'application/json'
    8. $content = $response->getContent();
    9. // $content = '{"id":521583, "name":"symfony-docs", ...}'
    10. $content = $response->toArray();
    11. // $content = ['id' => 521583, 'name' => 'symfony-docs', ...]

Tip

The HTTP client is interoperable with many common HTTP client abstractions in PHP. You can also use any of these abstractions to profit from autowirings. See Interoperability for more information.

Configuration

The HTTP client contains many options you might need to take full control of the way the request is performed, including DNS pre-resolution, SSL parameters, public key pinning, etc. They can be defined globally in the configuration (to apply it to all requests) and to each request (which overrides any global configuration).

You can configure the global options using the default_options option:

  • YAML

    1. # config/packages/framework.yaml
    2. framework:
    3. http_client:
    4. default_options:
    5. max_redirects: 7
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    9. <framework:config>
    10. <framework:http-client>
    11. <framework:default-options max-redirects="7"/>
    12. </framework:http-client>
    13. </framework:config>
    14. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->loadFromExtension('framework', [
    3. 'http_client' => [
    4. 'default_options' => [
    5. 'max_redirects' => 7,
    6. ],
    7. ],
    8. ]);
  • Standalone Use

    1. $client = HttpClient::create([
    2. 'max_redirects' => 7,
    3. ]);

Some options are described in this guide:

Check out the full http_client config reference to learn about all the options.

The HTTP client also has one configuration option called max_host_connections, this option can not be overridden by a request:

  • YAML

    1. # config/packages/framework.yaml
    2. framework:
    3. http_client:
    4. max_host_connections: 10
    5. # ...
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    9. <framework:config>
    10. <framework:http-client max-host-connections="10">
    11. <!-- ... -->
    12. </framework:http-client>
    13. </framework:config>
    14. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->loadFromExtension('framework', [
    3. 'http_client' => [
    4. 'max_host_connections' => 10,
    5. // ...
    6. ],
    7. ]);
  • Standalone Use

    1. $client = HttpClient::create([], 10);

Scoping Client

It’s common that some of the HTTP client options depend on the URL of the request (e.g. you must set some headers when making requests to GitHub API but not for other hosts). If that’s your case, the component provides scoped clients (using Symfony\Component\HttpClient\ScopingHttpClient) to autoconfigure the HTTP client based on the requested URL:

  • YAML

    1. # config/packages/framework.yaml
    2. framework:
    3. http_client:
    4. scoped_clients:
    5. # only requests matching scope will use these options
    6. github.client:
    7. scope: 'https://api\.github\.com'
    8. headers:
    9. Accept: 'application/vnd.github.v3+json'
    10. Authorization: 'token %env(GITHUB_API_TOKEN)%'
    11. # ...
    12. # using base_uri, relative URLs (e.g. request("GET", "/repos/symfony/symfony-docs"))
    13. # will default to these options
    14. github.client:
    15. base_uri: 'https://api.github.com'
    16. headers:
    17. Accept: 'application/vnd.github.v3+json'
    18. Authorization: 'token %env(GITHUB_API_TOKEN)%'
    19. # ...
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    9. <framework:config>
    10. <framework:http-client>
    11. <!-- only requests matching scope will use these options -->
    12. <framework:scoped-client name="github.client"
    13. scope="https://api\.github\.com"
    14. >
    15. <framework:header name="Accept">application/vnd.github.v3+json</framework:header>
    16. <framework:header name="Authorization">token %env(GITHUB_API_TOKEN)%</framework:header>
    17. </framework:scoped-client>
    18. <!-- using base-uri, relative URLs (e.g. request("GET", "/repos/symfony/symfony-docs"))
    19. will default to these options -->
    20. <framework:scoped-client name="github.client"
    21. base-uri="https://api.github.com"
    22. >
    23. <framework:header name="Accept">application/vnd.github.v3+json</framework:header>
    24. <framework:header name="Authorization">token %env(GITHUB_API_TOKEN)%</framework:header>
    25. </framework:scoped-client>
    26. </framework:http-client>
    27. </framework:config>
    28. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->loadFromExtension('framework', [
    3. 'http_client' => [
    4. 'scoped_clients' => [
    5. // only requests matching scope will use these options
    6. 'github.client' => [
    7. 'scope' => 'https://api\.github\.com',
    8. 'headers' => [
    9. 'Accept' => 'application/vnd.github.v3+json',
    10. 'Authorization' => 'token %env(GITHUB_API_TOKEN)%',
    11. ],
    12. // ...
    13. ],
    14. // using base_url, relative URLs (e.g. request("GET", "/repos/symfony/symfony-docs"))
    15. // will default to these options
    16. 'github.client' => [
    17. 'base_uri' => 'https://api.github.com',
    18. 'headers' => [
    19. 'Accept' => 'application/vnd.github.v3+json',
    20. 'Authorization' => 'token %env(GITHUB_API_TOKEN)%',
    21. ],
    22. // ...
    23. ],
    24. ],
    25. ],
    26. ]);
  • Standalone Use

    1. use Symfony\Component\HttpClient\HttpClient;
    2. use Symfony\Component\HttpClient\ScopingHttpClient;
    3. $client = HttpClient::create();
    4. $client = new ScopingHttpClient($client, [
    5. // the options defined as values apply only to the URLs matching
    6. // the regular expressions defined as keys
    7. 'https://api\.github\.com/' => [
    8. 'headers' => [
    9. 'Accept' => 'application/vnd.github.v3+json',
    10. 'Authorization' => 'token '.$githubToken,
    11. ],
    12. ],
    13. // ...
    14. ]);
    15. // relative URLs will use the 2nd argument as base URI and use the options of the 3rd argument
    16. $client = ScopingHttpClient::forBaseUri($client, 'https://api.github.com/', [
    17. 'headers' => [
    18. 'Accept' => 'application/vnd.github.v3+json',
    19. 'Authorization' => 'token '.$githubToken,
    20. ],
    21. ]);

You can define several scopes, so that each set of options is added only if a requested URL matches one of the regular expressions set by the scope option.

If you use scoped clients in the Symfony framework, you must use any of the methods defined by Symfony to choose a specific service. Each client has a unique service named after its configuration.

Each scoped client also defines a corresponding named autowiring alias. If you use for example Symfony\Contracts\HttpClient\HttpClientInterface $githubClient as the type and name of an argument, autowiring will inject the github.client service into your autowired classes.

Note

Read the base_uri option docs to learn the rules applied when merging relative URIs into the base URI of the scoped client.

Making Requests

The HTTP client provides a single `request() method to perform all kinds of HTTP requests:

  1. $response = $client->request('GET', 'https://...');
  2. $response = $client->request('POST', 'https://...');
  3. $response = $client->request('PUT', 'https://...');
  4. // ...
  5. // you can add request options (or override global ones) using the 3rd argument
  6. $response = $client->request('GET', 'https://...', [
  7. 'headers' => [
  8. 'Accept' => 'application/json',
  9. ],
  10. ]);

Responses are always asynchronous, so that the call to the method returns immediately instead of waiting to receive the response:

  1. // code execution continues immediately; it doesn't wait to receive the response
  2. $response = $client->request('GET', 'http://releases.ubuntu.com/18.04.2/ubuntu-18.04.2-desktop-amd64.iso');
  3. // getting the response headers waits until they arrive
  4. $contentType = $response->getHeaders()['content-type'][0];
  5. // trying to get the response content will block the execution until
  6. // the full response content is received
  7. $content = $response->getContent();

This component also supports streaming responses for full asynchronous applications.

Note

HTTP compression and chunked transfer encoding are automatically enabled when both your PHP runtime and the remote server support them.

Authentication

The HTTP client supports different authentication mechanisms. They can be defined globally in the configuration (to apply it to all requests) and to each request (which overrides any global authentication):

  • YAML

    1. # config/packages/framework.yaml
    2. framework:
    3. http_client:
    4. scoped_clients:
    5. example_api:
    6. base_uri: 'https://example.com/'
    7. # HTTP Basic authentication
    8. auth_basic: 'the-username:the-password'
    9. # HTTP Bearer authentication (also called token authentication)
    10. auth_bearer: the-bearer-token
    11. # Microsoft NTLM authentication
    12. auth_ntlm: 'the-username:the-password'
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    9. <framework:config>
    10. <framework:http-client>
    11. <!-- Available authentication options:
    12. auth-basic: HTTP Basic authentication
    13. auth-bearer: HTTP Bearer authentication (also called token authentication)
    14. auth-ntlm: Microsoft NTLM authentication -->
    15. <framework:scoped-client name="example_api"
    16. base-uri="https://example.com/"
    17. auth-basic="the-username:the-password"
    18. auth-bearer="the-bearer-token"
    19. auth-ntlm="the-username:the-password"
    20. />
    21. </framework:http-client>
    22. </framework:config>
    23. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->loadFromExtension('framework', [
    3. 'http_client' => [
    4. 'scoped_clients' => [
    5. 'example_api' => [
    6. 'base_uri' => 'https://example.com/',
    7. // HTTP Basic authentication
    8. 'auth_basic' => 'the-username:the-password',
    9. // HTTP Bearer authentication (also called token authentication)
    10. 'auth_bearer' => 'the-bearer-token',
    11. // Microsoft NTLM authentication
    12. 'auth_ntlm' => 'the-username:the-password',
    13. ],
    14. ],
    15. ],
    16. ]);
  • Standalone Use

    1. $client = HttpClient::createForBaseUri('https://example.com/', [
    2. // HTTP Basic authentication (there are multiple ways of configuring it)
    3. 'auth_basic' => ['the-username'],
    4. 'auth_basic' => ['the-username', 'the-password'],
    5. 'auth_basic' => 'the-username:the-password',
    6. // HTTP Bearer authentication (also called token authentication)
    7. 'auth_bearer' => 'the-bearer-token',
    8. // Microsoft NTLM authentication (there are multiple ways of configuring it)
    9. 'auth_ntlm' => ['the-username'],
    10. 'auth_ntlm' => ['the-username', 'the-password'],
    11. 'auth_ntlm' => 'the-username:the-password',
    12. ]);
  1. $response = $client->request('GET', 'https://...', [
  2. // use a different HTTP Basic authentication only for this request
  3. 'auth_basic' => ['the-username', 'the-password'],
  4. // ...
  5. ]);

Note

The NTLM authentication mechanism requires using the cURL transport. By using `HttpClient::createForBaseUri(), we ensure that the auth credentials won’t be sent to any other hosts than https://example.com/.

New in version 4.4: The auth_ntlm option and the `HttpClient::createForBaseUri() method were introduced in Symfony 4.4.

Query String Parameters

You can either append them manually to the requested URL, or define them as an associative array via the query option, that will be merged with the URL:

  1. // it makes an HTTP GET request to https://httpbin.org/get?token=...&name=...
  2. $response = $client->request('GET', 'https://httpbin.org/get', [
  3. // these values are automatically encoded before including them in the URL
  4. 'query' => [
  5. 'token' => '...',
  6. 'name' => '...',
  7. ],
  8. ]);

Headers

Use the headers option to define the default headers added to all requests:

  • YAML

    1. # config/packages/framework.yaml
    2. framework:
    3. http_client:
    4. default_options:
    5. headers:
    6. 'User-Agent': 'My Fancy App'
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    9. <framework:config>
    10. <framework:http-client>
    11. <framework:default-options>
    12. <framework:header name="User-Agent">My Fancy App</framework:header>
    13. </framework:default-options>
    14. </framework:http-client>
    15. </framework:config>
    16. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->loadFromExtension('framework', [
    3. 'http_client' => [
    4. 'default_options' => [
    5. 'headers' => [
    6. 'User-Agent' => 'My Fancy App',
    7. ],
    8. ],
    9. ],
    10. ]);
  • Standalone Use

    1. // this header is added to all requests made by this client
    2. $client = HttpClient::create([
    3. 'headers' => [
    4. 'User-Agent' => 'My Fancy App',
    5. ],
    6. ]);

You can also set new headers or override the default ones for specific requests:

  1. // this header is only included in this request and overrides the value
  2. // of the same header if defined globally by the HTTP client
  3. $response = $client->request('POST', 'https://...', [
  4. 'headers' => [
  5. 'Content-Type' => 'text/plain',
  6. ],
  7. ]);

Uploading Data

This component provides several methods for uploading data using the body option. You can use regular strings, closures, iterables and resources and they’ll be processed automatically when making the requests:

  1. $response = $client->request('POST', 'https://...', [
  2. // defining data using a regular string
  3. 'body' => 'raw data',
  4. // defining data using an array of parameters
  5. 'body' => ['parameter1' => 'value1', '...'],
  6. // using a closure to generate the uploaded data
  7. 'body' => function (int $size): string {
  8. // ...
  9. },
  10. // using a resource to get the data from it
  11. 'body' => fopen('/path/to/file', 'r'),
  12. ]);

When uploading data with the POST method, if you don’t define the Content-Type HTTP header explicitly, Symfony assumes that you’re uploading form data and adds the required 'Content-Type: application/x-www-form-urlencoded' header for you.

When the body option is set as a closure, it will be called several times until it returns the empty string, which signals the end of the body. Each time, the closure should return a string smaller than the amount requested as argument.

A generator or any Traversable can also be used instead of a closure.

Tip

When uploading JSON payloads, use the json option instead of body. The given content will be JSON-encoded automatically and the request will add the Content-Type: application/json automatically too:

  1. $response = $client->request('POST', 'https://...', [
  2. 'json' => ['param1' => 'value1', '...'],
  3. ]);
  4. $decodedPayload = $response->toArray();

To submit a form with file uploads, it is your responsibility to encode the body according to the multipart/form-data content-type. The Symfony Mime component makes it a few lines of code:

  1. use Symfony\Component\Mime\Part\DataPart;
  2. use Symfony\Component\Mime\Part\Multipart\FormDataPart;
  3. $formFields = [
  4. 'regular_field' => 'some value',
  5. 'file_field' => DataPart::fromPath('/path/to/uploaded/file'),
  6. ];
  7. $formData = new FormDataPart($formFields);
  8. $client->request('POST', 'https://...', [
  9. 'headers' => $formData->getPreparedHeaders()->toArray(),
  10. 'body' => $formData->bodyToIterable(),
  11. ]);

By default, HttpClient streams the body contents when uploading them. This might not work with all servers, resulting in HTTP status code 411 (“Length Required”) because there is no Content-Length header. The solution is to turn the body into a string with the following method (which will increase memory consumption when the streams are large):

  1. $client->request('POST', 'https://...', [
  2. // ...
  3. 'body' => $formData->bodyToString(),
  4. ]);

If you need to add a custom HTTP header to the upload, you can do:

  1. $headers = $formData->getPreparedHeaders()->toArray();
  2. $headers[] = 'X-Foo: bar';

Cookies

The HTTP client provided by this component is stateless but handling cookies requires a stateful storage (because responses can update cookies and they must be used for subsequent requests). That’s why this component doesn’t handle cookies automatically.

You can either handle cookies yourself using the Cookie HTTP header or use the BrowserKit component which provides this feature and integrates seamlessly with the HttpClient component.

Redirects

By default, the HTTP client follows redirects, up to a maximum of 20, when making a request. Use the max_redirects setting to configure this behavior (if the number of redirects is higher than the configured value, you’ll get a Symfony\Component\HttpClient\Exception\RedirectionException):

  1. $response = $client->request('GET', 'https://...', [
  2. // 0 means to not follow any redirect
  3. 'max_redirects' => 0,
  4. ]);

HTTP Proxies

By default, this component honors the standard environment variables that your Operating System defines to direct the HTTP traffic through your local proxy. This means there is usually nothing to configure to have the client work with proxies, provided these env vars are properly configured.

You can still set or override these settings using the proxy and no_proxy options:

  • proxy should be set to the http://... URL of the proxy to get through
  • no_proxy disables the proxy for a comma-separated list of hosts that do not require it to get reached.

Progress Callback

By providing a callable to the on_progress option, one can track uploads/downloads as they complete. This callback is guaranteed to be called on DNS resolution, on arrival of headers and on completion; additionally it is called when new data is uploaded or downloaded and at least once per second:

  1. $response = $client->request('GET', 'https://...', [
  2. 'on_progress' => function (int $dlNow, int $dlSize, array $info): void {
  3. // $dlNow is the number of bytes downloaded so far
  4. // $dlSize is the total size to be downloaded or -1 if it is unknown
  5. // $info is what $response->getInfo() would return at this very time
  6. },
  7. ]);

Any exceptions thrown from the callback will be wrapped in an instance of TransportExceptionInterface and will abort the request.

HTTPS Certificates

HttpClient uses the system’s certificate store to validate SSL certificates (while browsers use their own stores). When using self-signed certificates during development, it’s recommended to create your own certificate authority (CA) and add it to your system’s store.

Alternatively, you can also disable verify_host and verify_peer (see http_client config reference), but this is not recommended in production.

Performance

The component is built for maximum HTTP performance. By design, it is compatible with HTTP/2 and with doing concurrent asynchronous streamed and multiplexed requests/responses. Even when doing regular synchronous calls, this design allows keeping connections to remote hosts open between requests, improving performance by saving repetitive DNS resolution, SSL negotiation, etc. To leverage all these design benefits, the cURL extension is needed.

Enabling cURL Support

This component supports both the native PHP streams and cURL to make the HTTP requests. Although both are interchangeable and provide the same features, including concurrent requests, HTTP/2 is only supported when using cURL.

`HttpClient::create() selects the cURL transport if the cURL PHP extension is enabled and falls back to PHP streams otherwise. If you prefer to select the transport explicitly, use the following classes to create the client:

  1. use Symfony\Component\HttpClient\CurlHttpClient;
  2. use Symfony\Component\HttpClient\NativeHttpClient;
  3. // uses native PHP streams
  4. $client = new NativeHttpClient();
  5. // uses the cURL PHP extension
  6. $client = new CurlHttpClient();

When using this component in a full-stack Symfony application, this behavior is not configurable and cURL will be used automatically if the cURL PHP extension is installed and enabled. Otherwise, the native PHP streams will be used.

HTTP/2 Support

When requesting an https URL, HTTP/2 is enabled by default if libcurl >= 7.36 is used. To force HTTP/2 for http URLs, you need to enable it explicitly via the http_version option:

  • YAML

    1. # config/packages/framework.yaml
    2. framework:
    3. http_client:
    4. default_options:
    5. http_version: '2.0'
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    9. <framework:config>
    10. <framework:http-client>
    11. <framework:default-options http-version="2.0"/>
    12. </framework:http-client>
    13. </framework:config>
    14. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->loadFromExtension('framework', [
    3. 'http_client' => [
    4. 'default_options' => [
    5. 'http_version' => '2.0',
    6. ],
    7. ],
    8. ]);
  • Standalone Use

    1. $client = HttpClient::create(['http_version' => '2.0']);

Support for HTTP/2 PUSH works out of the box when libcurl >= 7.61 is used with PHP >= 7.2.17 / 7.3.4: pushed responses are put into a temporary cache and are used when a subsequent request is triggered for the corresponding URLs.

Processing Responses

The response returned by all HTTP clients is an object of type Symfony\Contracts\HttpClient\ResponseInterface which provides the following methods:

  1. $response = $client->request('GET', 'https://...');
  2. // gets the HTTP status code of the response
  3. $statusCode = $response->getStatusCode();
  4. // gets the HTTP headers as string[][] with the header names lower-cased
  5. $headers = $response->getHeaders();
  6. // gets the response body as a string
  7. $content = $response->getContent();
  8. // casts the response JSON content to a PHP array
  9. $content = $response->toArray();
  10. // casts the response content to a PHP stream resource
  11. $content = $response->toStream();
  12. // cancels the request/response
  13. $response->cancel();
  14. // returns info coming from the transport layer, such as "response_headers",
  15. // "redirect_count", "start_time", "redirect_url", etc.
  16. $httpInfo = $response->getInfo();
  17. // you can get individual info too
  18. $startTime = $response->getInfo('start_time');
  19. // e.g. this returns the final response URL (resolving redirections if needed)
  20. $url = $response->getInfo('url');
  21. // returns detailed logs about the requests and responses of the HTTP transaction
  22. $httpLogs = $response->getInfo('debug');

New in version 4.4: The `toStream() method was introduced in Symfony 4.4.

Note

$response->getInfo() is non-blocking: it returns *live* information about the response. Some of them might not be known yet (e.g.http_code`) when you’ll call it.

Streaming Responses

Call the `stream() method of the HTTP client to get chunks of the response sequentially instead of waiting for the entire response:

  1. $url = 'https://releases.ubuntu.com/18.04.1/ubuntu-18.04.1-desktop-amd64.iso';
  2. $response = $client->request('GET', $url);
  3. // Responses are lazy: this code is executed as soon as headers are received
  4. if (200 !== $response->getStatusCode()) {
  5. throw new \Exception('...');
  6. }
  7. // get the response content in chunks and save them in a file
  8. // response chunks implement Symfony\Contracts\HttpClient\ChunkInterface
  9. $fileHandler = fopen('/ubuntu.iso', 'w');
  10. foreach ($client->stream($response) as $chunk) {
  11. fwrite($fileHandler, $chunk->getContent());
  12. }

Note

By default, text/*, JSON and XML response bodies are buffered in a local php://temp stream. You can control this behavior by using the buffer option: set it to true/false to enable/disable buffering, or to a closure that should return the same based on the response headers it receives as argument.

Canceling Responses

To abort a request (e.g. because it didn’t complete in due time, or you want to fetch only the first bytes of the response, etc.), you can either use the cancel() method ofResponseInterface`:

  1. $response->cancel();

Or throw an exception from a progress callback:

  1. $response = $client->request('GET', 'https://...', [
  2. 'on_progress' => function (int $dlNow, int $dlSize, array $info): void {
  3. // ...
  4. throw new \MyException();
  5. },
  6. ]);

The exception will be wrapped in an instance of TransportExceptionInterface and will abort the request.

In case the response was canceled using $response->cancel(),$response->getInfo(‘canceled’) will return true.

New in version 4.4: The possibility to get information about canceled or not was introduced in Symfony 4.4.

Handling Exceptions

When the HTTP status code of the response is in the 300-599 range (i.e. 3xx, 4xx or 5xx) your code is expected to handle it. If you don’t do that, the getHeaders(),getContent() and toArray() methods throw an appropriate exception, all of which implement theSymfony\Contracts\HttpClient\Exception\HttpExceptionInterface`:

  1. // the response of this request will be a 403 HTTP error
  2. $response = $client->request('GET', 'https://httpbin.org/status/403');
  3. // this code results in a Symfony\Component\HttpClient\Exception\ClientException
  4. // because it doesn't check the status code of the response
  5. $content = $response->getContent();
  6. // pass FALSE as the optional argument to not throw an exception and return
  7. // instead the original response content (even if it's an error message)
  8. $content = $response->getContent(false);

While responses are lazy, their destructor will always wait for headers to come back. This means that the following request will complete; and if e.g. a 404 is returned, an exception will be thrown:

  1. // because the returned value is not assigned to a variable, the destructor
  2. // of the returned response will be called immediately and will throw if the
  3. // status code is in the 300-599 range
  4. $client->request('POST', 'https://...');

This in turn means that unassigned responses will fallback to synchronous requests. If you want to make these requests concurrent, you can store their corresponding responses in an array:

  1. $responses[] = $client->request('POST', 'https://.../path1');
  2. $responses[] = $client->request('POST', 'https://.../path2');
  3. // ...
  4. // This line will trigger the destructor of all responses stored in the array;
  5. // they will complete concurrently and an exception will be thrown in case a
  6. // status code in the 300-599 range is returned
  7. unset($responses);

This behavior provided at destruction-time is part of the fail-safe design of the component. No errors will be unnoticed: if you don’t write the code to handle errors, exceptions will notify you when needed. On the other hand, if you write the error-handling code, you will opt-out from these fallback mechanisms as the destructor won’t have anything remaining to do.

There are three types of exceptions:

  • Exceptions implementing the Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface are thrown when your code does not handle the status codes in the 300-599 range.
  • Exceptions implementing the Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface are thrown when a lower level issue occurs.
  • Exceptions implementing the Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface are thrown when a content-type cannot be decoded to the expected representation.

Concurrent Requests

Thanks to responses being lazy, requests are always managed concurrently. On a fast enough network, the following code makes 379 requests in less than half a second when cURL is used:

  1. $responses = [];
  2. for ($i = 0; $i < 379; ++$i) {
  3. $uri = "https://http2.akamai.com/demo/tile-$i.png";
  4. $responses[] = $client->request('GET', $uri);
  5. }
  6. foreach ($responses as $response) {
  7. $content = $response->getContent();
  8. // ...
  9. }

As you can read in the first “for” loop, requests are issued but are not consumed yet. That’s the trick when concurrency is desired: requests should be sent first and be read later on. This will allow the client to monitor all pending requests while your code waits for a specific one, as done in each iteration of the above “foreach” loop.

Note

The maximum number of concurrent requests that you can perform depends on the resources of your machine (e.g. your operating system may limit the number of simultaneous reads of the file that stores the certificates file). Make your requests in batches to avoid these issues.

Multiplexing Responses

If you look again at the snippet above, responses are read in requests’ order. But maybe the 2nd response came back before the 1st? Fully asynchronous operations require being able to deal with the responses in whatever order they come back.

In order to do so, the `stream() method of HTTP clients accepts a list of responses to monitor. As mentioned previously, this method yields response chunks as they arrive from the network. By replacing the “foreach” in the snippet with this one, the code becomes fully async:

  1. foreach ($client->stream($responses) as $response => $chunk) {
  2. if ($chunk->isFirst()) {
  3. // headers of $response just arrived
  4. // $response->getHeaders() is now a non-blocking call
  5. } elseif ($chunk->isLast()) {
  6. // the full content of $response just completed
  7. // $response->getContent() is now a non-blocking call
  8. } else {
  9. // $chunk->getContent() will return a piece
  10. // of the response body that just arrived
  11. }
  12. }

Tip

Use the user_data option combined with `$response->getInfo(‘user_data’) to track the identity of the responses in your foreach loops.

Dealing with Network Timeouts

This component allows dealing with both request and response timeouts.

A timeout can happen when e.g. DNS resolution takes too much time, when the TCP connection cannot be opened in the given time budget, or when the response content pauses for too long. This can be configured with the timeout request option:

  1. // A TransportExceptionInterface will be issued if nothing
  2. // happens for 2.5 seconds when accessing from the $response
  3. $response = $client->request('GET', 'https://...', ['timeout' => 2.5]);

The default_socket_timeout PHP ini setting is used if the option is not set.

The option can be overridden by using the 2nd argument of the stream() method. This allows monitoring several responses at once and applying the timeout to all of them in a group. If all responses become inactive for the given duration, the method will yield a special chunk whoseisTimeout() will return true:

  1. foreach ($client->stream($responses, 1.5) as $response => $chunk) {
  2. if ($chunk->isTimeout()) {
  3. // $response staled for more than 1.5 seconds
  4. }
  5. }

A timeout is not necessarily an error: you can decide to stream again the response and get remaining contents that might come back in a new timeout, etc.

Tip

Passing 0 as timeout allows monitoring responses in a non-blocking way.

Note

Timeouts control how long one is willing to wait while the HTTP transaction is idle. Big responses can last as long as needed to complete, provided they remain active during the transfer and never pause for longer than specified.

Use the max_duration option to limit the time a full request/response can last.

Dealing with Network Errors

Network errors (broken pipe, failed DNS resolution, etc.) are thrown as instances of Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface.

First of all, you don’t have to deal with them: letting errors bubble to your generic exception-handling stack might be really fine in most use cases.

If you want to handle them, here is what you need to know:

To catch errors, you need to wrap calls to $client->request() but also calls to any methods of the returned responses. This is because responses are lazy, so that network errors can happen when calling e.g.getStatusCode() too:

  1. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  2. // ...
  3. try {
  4. // both lines can potentially throw
  5. $response = $client->request(...);
  6. $headers = $response->getHeaders();
  7. // ...
  8. } catch (TransportExceptionInterface $e) {
  9. // ...
  10. }

Note

Because `$response->getInfo() is non-blocking, it shouldn’t throw by design.

When multiplexing responses, you can deal with errors for individual streams by catching TransportExceptionInterface in the foreach loop:

  1. foreach ($client->stream($responses) as $response => $chunk) {
  2. try {
  3. if ($chunk->isTimeout()) {
  4. // ... decide what to do when a timeout occurs
  5. // if you want to stop a response that timed out, don't miss
  6. // calling $response->cancel() or the destructor of the response
  7. // will try to complete it one more time
  8. } elseif ($chunk->isFirst()) {
  9. // if you want to check the status code, you must do it when the
  10. // first chunk arrived, using $response->getStatusCode();
  11. // not doing so might trigger an HttpExceptionInterface
  12. } elseif ($chunk->isLast()) {
  13. // ... do something with $response
  14. }
  15. } catch (TransportExceptionInterface $e) {
  16. // ...
  17. }
  18. }

Caching Requests and Responses

This component provides a Symfony\Component\HttpClient\CachingHttpClient decorator that allows caching responses and serving them from the local storage for next requests. The implementation leverages the Symfony\Component\HttpKernel\HttpCache\HttpCache class under the hood so that the HttpKernel component needs to be installed in your application:

  1. use Symfony\Component\HttpClient\CachingHttpClient;
  2. use Symfony\Component\HttpClient\HttpClient;
  3. use Symfony\Component\HttpKernel\HttpCache\Store;
  4. $store = new Store('/path/to/cache/storage/');
  5. $client = HttpClient::create();
  6. $client = new CachingHttpClient($client, $store);
  7. // this won't hit the network if the resource is already in the cache
  8. $response = $client->request('GET', 'https://example.com/cacheable-resource');

CachingHttpClient accepts a third argument to set the options of the HttpCache.

Interoperability

The component is interoperable with four different abstractions for HTTP clients: Symfony Contracts, PSR-18, HTTPlug v1/v2 and native PHP streams. If your application uses libraries that need any of them, the component is compatible with all of them. They also benefit from autowiring aliases when the framework bundle is used.

If you are writing or maintaining a library that makes HTTP requests, you can decouple it from any specific HTTP client implementations by coding against either Symfony Contracts (recommended), PSR-18 or HTTPlug v2.

Symfony Contracts

The interfaces found in the symfony/http-client-contracts package define the primary abstractions implemented by the component. Its entry point is the Symfony\Contracts\HttpClient\HttpClientInterface. That’s the interface you need to code against when a client is needed:

  1. use Symfony\Contracts\HttpClient\HttpClientInterface;
  2. class MyApiLayer
  3. {
  4. private $client;
  5. public function __construct(HttpClientInterface $client)
  6. {
  7. $this->client = $client;
  8. }
  9. // [...]
  10. }

All request options mentioned above (e.g. timeout management) are also defined in the wordings of the interface, so that any compliant implementations (like this component) is guaranteed to provide them. That’s a major difference with the other abstractions, which provide none related to the transport itself.

Another major feature covered by the Symfony Contracts is async/multiplexing, as described in the previous sections.

PSR-18 and PSR-17

This component implements the PSR-18 (HTTP Client) specifications via the Symfony\Component\HttpClient\Psr18Client class, which is an adapter to turn a Symfony HttpClientInterface into a PSR-18 ClientInterface. This class also implements the relevant methods of PSR-17 to ease creating request objects.

To use it, you need the psr/http-client package and a PSR-17 implementation:

  1. # installs the PSR-18 ClientInterface
  2. $ composer require psr/http-client
  3. # installs an efficient implementation of response and stream factories
  4. # with autowiring aliases provided by Symfony Flex
  5. $ composer require nyholm/psr7
  6. # alternatively, install the php-http/discovery package to auto-discover
  7. # any already installed implementations from common vendors:
  8. # composer require php-http/discovery

Now you can make HTTP requests with the PSR-18 client as follows:

  • Framework Use

    1. use Psr\Http\Client\ClientInterface;
    2. class Symfony
    3. {
    4. private $client;
    5. public function __construct(ClientInterface $client)
    6. {
    7. $this->client = $client;
    8. }
    9. public function getAvailableVersions(): array
    10. {
    11. $request = $this->client->createRequest('GET', 'https://symfony.com/versions.json');
    12. $response = $this->client->sendRequest($request);
    13. return json_decode($response->getBody()->getContents(), true);
    14. }
    15. }
  • Standalone Use

    1. use Symfony\Component\HttpClient\Psr18Client;
    2. $client = new Psr18Client();
    3. $request = $client->createRequest('GET', 'https://symfony.com/versions.json');
    4. $response = $client->sendRequest($request);
    5. $content = json_decode($response->getBody()->getContents(), true);

New in version 4.4: The PSR-17 factory methods of Psr18Client were introduced in Symfony 4.4.

HTTPlug

New in version 4.4: Support for HTTPlug was introduced in Symfony 4.4.

The HTTPlug v1 specification was published before PSR-18 and is superseded by it. As such, you should not use it in newly written code. The component is still interoperable with libraries that require it thanks to the Symfony\Component\HttpClient\HttplugClient class. Similarly to Psr18Client implementing relevant parts of PSR-17, HttplugClient also implements the factory methods defined in the related php-http/message-factory package.

  1. # Let's suppose php-http/httplug is already required by the lib you want to use
  2. # installs an efficient implementation of response and stream factories
  3. # with autowiring aliases provided by Symfony Flex
  4. $ composer require nyholm/psr7
  5. # alternatively, install the php-http/discovery package to auto-discover
  6. # any already installed implementations from common vendors:
  7. # composer require php-http/discovery

Let’s say you want to instantiate a class with the following constructor, that requires HTTPlug dependencies:

  1. use Http\Client\HttpClient;
  2. use Http\Message\RequestFactory;
  3. use Http\Message\StreamFactory;
  4. class SomeSdk
  5. {
  6. public function __construct(
  7. HttpClient $httpClient,
  8. RequestFactory $requestFactory,
  9. StreamFactory $streamFactory
  10. )
  11. // [...]
  12. }

Because HttplugClient implements the three interfaces, you can use it this way:

  1. use Symfony\Component\HttpClient\HttplugClient;
  2. $httpClient = new HttplugClient();
  3. $apiClient = new SomeSdk($httpClient, $httpClient, $httpClient);

If you’d like to work with promises, HttplugClient also implements the HttpAsyncClient interface. To use it, you need to install the guzzlehttp/promises package:

  1. $ composer require guzzlehttp/promises

Then you’re ready to go:

  1. use Psr\Http\Message\ResponseInterface;
  2. use Symfony\Component\HttpClient\HttplugClient;
  3. $httpClient = new HttplugClient();
  4. $request = $httpClient->createRequest('GET', 'https://my.api.com/');
  5. $promise = $httpClient->sendAsyncRequest($request)
  6. ->then(
  7. function (ResponseInterface $response) {
  8. echo 'Got status '.$response->getStatusCode();
  9. return $response;
  10. },
  11. function (\Throwable $exception) {
  12. echo 'Error: '.$exception->getMessage();
  13. throw $exception;
  14. }
  15. );
  16. // after you're done with sending several requests,
  17. // you must wait for them to complete concurrently
  18. // wait for a specific promise to resolve while monitoring them all
  19. $response = $promise->wait();
  20. // wait maximum 1 second for pending promises to resolve
  21. $httpClient->wait(1.0);
  22. // wait for all remaining promises to resolve
  23. $httpClient->wait();

Native PHP Streams

New in version 4.4: Support for native PHP streams was introduced in Symfony 4.4.

Responses implementing Symfony\Contracts\HttpClient\ResponseInterface can be cast to native PHP streams with createResource(). This allows using them where native PHP streams are needed:

  1. use Symfony\Component\HttpClient\HttpClient;
  2. use Symfony\Component\HttpClient\Response\StreamWrapper;
  3. $client = HttpClient::create();
  4. $response = $client->request('GET', 'https://symfony.com/versions.json');
  5. $streamResource = StreamWrapper::createResource($response, $client);
  6. // alternatively and contrary to the previous one, this returns
  7. // a resource that is seekable and potentially stream_select()-able
  8. $streamResource = $response->toStream();
  9. echo stream_get_contents($streamResource); // outputs the content of the response
  10. // later on if you need to, you can access the response from the stream
  11. $response = stream_get_meta_data($streamResource)['wrapper_data']->getResponse();

Testing HTTP Clients and Responses

This component includes the MockHttpClient and MockResponse classes to use them in tests that need an HTTP client which doesn’t make actual HTTP requests.

The first way of using MockHttpClient is to pass a list of responses to its constructor. These will be yielded in order when requests are made:

  1. use Symfony\Component\HttpClient\MockHttpClient;
  2. use Symfony\Component\HttpClient\Response\MockResponse;
  3. $responses = [
  4. new MockResponse($body1, $info1),
  5. new MockResponse($body2, $info2),
  6. ];
  7. $client = new MockHttpClient($responses);
  8. // responses are returned in the same order as passed to MockHttpClient
  9. $response1 = $client->request('...'); // returns $responses[0]
  10. $response2 = $client->request('...'); // returns $responses[1]

Another way of using MockHttpClient is to pass a callback that generates the responses dynamically when it’s called:

  1. use Symfony\Component\HttpClient\MockHttpClient;
  2. use Symfony\Component\HttpClient\Response\MockResponse;
  3. $callback = function ($method, $url, $options) {
  4. return new MockResponse('...');
  5. };
  6. $client = new MockHttpClient($callback);
  7. $response = $client->request('...'); // calls $callback to get the response

If you need to test responses with HTTP status codes different than 200, define the http_code option:

  1. use Symfony\Component\HttpClient\MockHttpClient;
  2. use Symfony\Component\HttpClient\Response\MockResponse;
  3. $client = new MockHttpClient([
  4. new MockResponse('...', ['http_code' => 500]),
  5. new MockResponse('...', ['http_code' => 404]),
  6. ]);
  7. $response = $client->request('...');

The responses provided to the mock client don’t have to be instances of MockResponse. Any class implementing ResponseInterface will work (e.g. `$this->createMock(ResponseInterface::class)).

However, using MockResponse allows simulating chunked responses and timeouts:

  1. $body = function () {
  2. yield 'hello';
  3. // empty strings are turned into timeouts so that they are easy to test
  4. yield '';
  5. yield 'world';
  6. };
  7. $mockResponse = new MockResponse($body());

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.