ResponseInterface.php 4.3 KB

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