ResponseInterface.php 4.8 KB

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