| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <[email protected]> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Contracts\HttpClient; |
| 13 | |
| 14 | use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 15 | use Symfony\Contracts\HttpClient\Test\HttpClientTestCase; |
| 16 | |
| 17 | /** |
| 18 | * Provides flexible methods for requesting HTTP resources synchronously or asynchronously. |
| 19 | * |
| 20 | * @see HttpClientTestCase for a reference test suite |
| 21 | * |
| 22 | * @author Nicolas Grekas <[email protected]> |
| 23 | */ |
| 24 | interface HttpClientInterface |
| 25 | { |
| 26 | public const OPTIONS_DEFAULTS = [ |
| 27 | // array|string - an array containing the username as first value, and optionally the |
| 28 | // password as the second one; or string like username:password - enabling HTTP Basic |
| 29 | // authentication (RFC 7617) |
| 30 | 'auth_basic' => null, |
| 31 | // string - a token enabling HTTP Bearer authorization (RFC 6750) |
| 32 | 'auth_bearer' => null, |
| 33 | // string[] - associative array of query string values to merge with the request's URL |
| 34 | 'query' => [], |
| 35 | // iterable|string[]|string[][] - headers names provided as keys or as part of values |
| 36 | 'headers' => [], |
| 37 | // array|string|resource|\Traversable|\Closure - the callback SHOULD yield a string |
| 38 | // smaller than the amount requested as argument; the empty string signals EOF; if |
| 39 | // an array is passed, it is meant as a form payload of field names and values |
| 40 | 'body' => '', |
| 41 | // mixed - if set, implementations MUST set the "body" option to the JSON-encoded |
| 42 | // value and set the "content-type" header to a JSON-compatible value if it is not |
| 43 | // explicitly defined in the headers option - typically "application/json" |
| 44 | 'json' => null, |
| 45 | // mixed - any extra data to attach to the request (scalar, callable, object...) that |
| 46 | // MUST be available via $response->getInfo('user_data') - not used internally |
| 47 | 'user_data' => null, |
| 48 | // int - the maximum number of redirects to follow; a value lower than or equal to 0 |
| 49 | // means redirects should not be followed; "Authorization" and "Cookie" headers MUST |
| 50 | // NOT follow except for the initial host name |
| 51 | 'max_redirects' => 20, |
| 52 | // string - defaults to the best supported version, typically 1.1 or 2.0 |
| 53 | 'http_version' => null, |
| 54 | // string - the URI to resolve relative URLs, following rules in RFC 3986, section 2 |
| 55 | 'base_uri' => null, |
| 56 | // bool|resource|\Closure - whether the content of the response should be buffered or not, |
| 57 | // or a stream resource where the response body should be written, |
| 58 | // or a closure telling if/where the response should be buffered based on its headers |
| 59 | 'buffer' => true, |
| 60 | // callable(int $dlNow, int $dlSize, array $info) - throwing any exceptions MUST abort the |
| 61 | // request; it MUST be called on connection, on headers and on completion; it SHOULD be |
| 62 | // called on upload/download of data and at least 1/s |
| 63 | 'on_progress' => null, |
| 64 | // string[] - a map of host to IP address that SHOULD replace DNS resolution |
| 65 | 'resolve' => [], |
| 66 | // string - by default, the proxy-related env vars handled by curl SHOULD be honored |
| 67 | 'proxy' => null, |
| 68 | // string - a comma separated list of hosts that do not require a proxy to be reached |
| 69 | 'no_proxy' => null, |
| 70 | // float - the idle timeout (in seconds) - defaults to ini_get('default_socket_timeout') |
| 71 | 'timeout' => null, |
| 72 | // float - the maximum execution time (in seconds) for the request+response as a whole; |
| 73 | // a value lower than or equal to 0 means it is unlimited |
| 74 | 'max_duration' => 0, |
| 75 | // float - the maximum duration (in seconds) allowed for DNS + TCP + TLS connection; |
| 76 | // a value lower than or equal to 0 means unlimited, as long as option timeout is respected |
| 77 | 'max_connect_duration' => 0, |
| 78 | // string - the interface or the local socket to bind to |
| 79 | 'bindto' => '0', |
| 80 | // see https://php.net/context.ssl for the following options |
| 81 | 'verify_peer' => true, |
| 82 | 'verify_host' => true, |
| 83 | 'cafile' => null, |
| 84 | 'capath' => null, |
| 85 | 'local_cert' => null, |
| 86 | 'local_pk' => null, |
| 87 | 'passphrase' => null, |
| 88 | 'ciphers' => null, |
| 89 | 'peer_fingerprint' => null, |
| 90 | 'capture_peer_cert_chain' => false, |
| 91 | // STREAM_CRYPTO_METHOD_TLSv*_CLIENT - minimum TLS version |
| 92 | 'crypto_method' => \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, |
| 93 | // array - additional options that can be ignored if unsupported, unlike regular options |
| 94 | 'extra' => [ |
| 95 | // bool - whether to use persistent connections where supported |
| 96 | 'use_persistent_connections' => false, |
| 97 | ], |
| 98 | ]; |
| 99 | |
| 100 | /** |
| 101 | * Requests an HTTP resource. |
| 102 | * |
| 103 | * Responses MUST be lazy, but their status code MUST be |
| 104 | * checked even if none of their public methods are called. |
| 105 | * |
| 106 | * Implementations are not required to support all options described above; they can also |
| 107 | * support more custom options; but in any case, they MUST throw a TransportExceptionInterface |
| 108 | * when an unsupported option is passed. |
| 109 | * |
| 110 | * @throws TransportExceptionInterface When an unsupported option is passed |
| 111 | */ |
| 112 | public function request(string $method, string $url, array $options = []): ResponseInterface; |
| 113 | |
| 114 | /** |
| 115 | * Yields responses chunk by chunk as they complete. |
| 116 | * |
| 117 | * @param ResponseInterface|iterable<array-key, ResponseInterface> $responses One or more responses created by the current HTTP client |
| 118 | * @param float|null $timeout The idle timeout before yielding timeout chunks |
| 119 | */ |
| 120 | public function stream(ResponseInterface|iterable $responses, ?float $timeout = null): ResponseStreamInterface; |
| 121 | |
| 122 | /** |
| 123 | * Returns a new instance of the client with new default options. |
| 124 | */ |
| 125 | public function withOptions(array $options): static; |
| 126 | } |