| 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\ClientExceptionInterface; |
| 15 | use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; |
| 16 | use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; |
| 17 | use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; |
| 18 | use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 19 | |
| 20 | /** |
| 21 | * A (lazily retrieved) HTTP response. |
| 22 | * |
| 23 | * @author Nicolas Grekas <[email protected]> |
| 24 | */ |
| 25 | interface ResponseInterface |
| 26 | { |
| 27 | /** |
| 28 | * Gets the HTTP status code of the response. |
| 29 | * |
| 30 | * @throws TransportExceptionInterface when a network error occurs |
| 31 | */ |
| 32 | public function getStatusCode(): int; |
| 33 | |
| 34 | /** |
| 35 | * Gets the HTTP headers of the response. |
| 36 | * |
| 37 | * @param bool $throw Whether an exception should be thrown on 3/4/5xx status codes |
| 38 | * |
| 39 | * @return array<string, list<string>> The headers of the response keyed by header names in lowercase |
| 40 | * |
| 41 | * @throws TransportExceptionInterface When a network error occurs |
| 42 | * @throws RedirectionExceptionInterface On a 3xx when $throw is true and the "max_redirects" option has been reached |
| 43 | * @throws ClientExceptionInterface On a 4xx when $throw is true |
| 44 | * @throws ServerExceptionInterface On a 5xx when $throw is true |
| 45 | */ |
| 46 | public function getHeaders(bool $throw = true): array; |
| 47 | |
| 48 | /** |
| 49 | * Gets the response body as a string. |
| 50 | * |
| 51 | * @param bool $throw Whether an exception should be thrown on 3/4/5xx status codes |
| 52 | * |
| 53 | * @throws TransportExceptionInterface When a network error occurs |
| 54 | * @throws RedirectionExceptionInterface On a 3xx when $throw is true and the "max_redirects" option has been reached |
| 55 | * @throws ClientExceptionInterface On a 4xx when $throw is true |
| 56 | * @throws ServerExceptionInterface On a 5xx when $throw is true |
| 57 | */ |
| 58 | public function getContent(bool $throw = true): string; |
| 59 | |
| 60 | /** |
| 61 | * Gets the response body decoded as array, typically from a JSON payload. |
| 62 | * |
| 63 | * @param bool $throw Whether an exception should be thrown on 3/4/5xx status codes |
| 64 | * |
| 65 | * @throws DecodingExceptionInterface When the body cannot be decoded to an array |
| 66 | * @throws TransportExceptionInterface When a network error occurs |
| 67 | * @throws RedirectionExceptionInterface On a 3xx when $throw is true and the "max_redirects" option has been reached |
| 68 | * @throws ClientExceptionInterface On a 4xx when $throw is true |
| 69 | * @throws ServerExceptionInterface On a 5xx when $throw is true |
| 70 | */ |
| 71 | public function toArray(bool $throw = true): array; |
| 72 | |
| 73 | /** |
| 74 | * Closes the response stream and all related buffers. |
| 75 | * |
| 76 | * No further chunk will be yielded after this method has been called. |
| 77 | */ |
| 78 | public function cancel(): void; |
| 79 | |
| 80 | /** |
| 81 | * Returns info coming from the transport layer. |
| 82 | * |
| 83 | * This method SHOULD NOT throw any ExceptionInterface and SHOULD be non-blocking. |
| 84 | * The returned info is "live": it can be empty and can change from one call to |
| 85 | * another, as the request/response progresses. |
| 86 | * |
| 87 | * The following info MUST be returned: |
| 88 | * - canceled (bool) - true if the response was canceled using ResponseInterface::cancel(), false otherwise |
| 89 | * - error (string|null) - the error message when the transfer was aborted, null otherwise |
| 90 | * - http_code (int) - the last response code or 0 when it is not known yet |
| 91 | * - http_method (string) - the HTTP verb of the last request |
| 92 | * - redirect_count (int) - the number of redirects followed while executing the request |
| 93 | * - redirect_url (string|null) - the resolved location of redirect responses, null otherwise |
| 94 | * - response_headers (array) - an array modelled after the special $http_response_header variable |
| 95 | * - start_time (float) - the time when the request was sent or 0.0 when it's pending |
| 96 | * - url (string) - the last effective URL of the request |
| 97 | * - user_data (mixed) - the value of the "user_data" request option, null if not set |
| 98 | * |
| 99 | * When the "capture_peer_cert_chain" option is true, the "peer_certificate_chain" |
| 100 | * attribute SHOULD list the peer certificates as an array of OpenSSL X.509 resources. |
| 101 | * |
| 102 | * Other info SHOULD be named after curl_getinfo()'s associative return value. |
| 103 | * |
| 104 | * @return mixed An array of all available info, or one of them when $type is |
| 105 | * provided, or null when an unsupported type is requested |
| 106 | */ |
| 107 | public function getInfo(?string $type = null): mixed; |
| 108 | } |