response.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ======================
  2. Using Response objects
  3. ======================
  4. Sending a request will return a ``Guzzle\Http\Message\Response`` object. You can view the raw HTTP response message by
  5. casting the Response object to a string. Casting the response to a string will return the entity body of the response
  6. as a string too, so this might be an expensive operation if the entity body is stored in a file or network stream. If
  7. you only want to see the response headers, you can call ``getRawHeaders()``.
  8. Response status line
  9. --------------------
  10. The different parts of a response's `status line <http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1>`_
  11. (the first line of the response HTTP message) are easily retrievable.
  12. .. code-block:: php
  13. $response = $client->get('http://www.amazon.com')->send();
  14. echo $response->getStatusCode(); // >>> 200
  15. echo $response->getReasonPhrase(); // >>> OK
  16. echo $response->getProtocol(); // >>> HTTP
  17. echo $response->getProtocolVersion(); // >>> 1.1
  18. You can determine the type of the response using several helper methods:
  19. .. code-block:: php
  20. $response->isSuccessful(); // true
  21. $response->isInformational();
  22. $response->isRedirect();
  23. $response->isClientError();
  24. $response->isServerError();
  25. Response headers
  26. ----------------
  27. The Response object contains helper methods for retrieving common response headers. These helper methods normalize the
  28. variations of HTTP response headers.
  29. .. code-block:: php
  30. $response->getCacheControl();
  31. $response->getContentType();
  32. $response->getContentLength();
  33. $response->getContentEncoding();
  34. $response->getContentMd5();
  35. $response->getEtag();
  36. // etc... There are methods for every known response header
  37. You can interact with the Response headers using the same exact methods used to interact with Request headers. See
  38. :ref:`http-message-headers` for more information.
  39. .. code-block:: php
  40. echo $response->getHeader('Content-Type');
  41. echo $response->getHeader('Content-Length');
  42. echo $response->getHeaders()['Content-Type']; // PHP 5.4
  43. Response body
  44. -------------
  45. The entity body object of a response can be retrieved by calling ``$response->getBody()``. The response EntityBody can
  46. be cast to a string, or you can pass ``true`` to this method to retrieve the body as a string.
  47. .. code-block:: php
  48. $request = $client->get('http://www.amazon.com');
  49. $response = $request->send();
  50. echo $response->getBody();
  51. See :doc:`/http-client/entity-bodies` for more information on entity bodies.
  52. JSON Responses
  53. ~~~~~~~~~~~~~~
  54. You can easily parse and use a JSON response as an array using the ``json()`` method of a response. This method will
  55. always return an array if the response is valid JSON or if the response body is empty. You will get an exception if you
  56. call this method and the response is not valid JSON.
  57. .. code-block:: php
  58. $data = $response->json();
  59. echo gettype($data);
  60. // >>> array
  61. XML Responses
  62. ~~~~~~~~~~~~~
  63. You can easily parse and use a XML response as SimpleXMLElement object using the ``xml()`` method of a response. This
  64. method will always return a SimpleXMLElement object if the response is valid XML or if the response body is empty. You
  65. will get an exception if you call this method and the response is not valid XML.
  66. .. code-block:: php
  67. $xml = $response->xml();
  68. echo $xml->foo;
  69. // >>> Bar!
  70. Streaming responses
  71. -------------------
  72. Some web services provide streaming APIs that allow a client to keep a HTTP request open for an extended period of
  73. time while polling and reading. Guzzle provides a simple way to convert HTTP request messages into
  74. ``Guzzle\Stream\Stream`` objects so that you can send the initial headers of a request, read the response headers, and
  75. pull in the response body manually as needed.
  76. Here's an example using the Twitter Streaming API to track the keyword "bieber":
  77. .. code-block:: php
  78. use Guzzle\Http\Client;
  79. use Guzzle\Stream\PhpStreamRequestFactory;
  80. $client = new Client('https://stream.twitter.com/1');
  81. $request = $client->post('statuses/filter.json', null, array(
  82. 'track' => 'bieber'
  83. ));
  84. $request->setAuth('myusername', 'mypassword');
  85. $factory = new PhpStreamRequestFactory();
  86. $stream = $factory->fromRequest($request);
  87. // Read until the stream is closed
  88. while (!$stream->feof()) {
  89. // Read a line from the stream
  90. $line = $stream->readLine();
  91. // JSON decode the line of data
  92. $data = json_decode($line, true);
  93. }
  94. You can use the ``stream`` request option when using a static client to more easily create a streaming response.
  95. .. code-block:: php
  96. $stream = Guzzle::get('http://guzzlephp.org', array('stream' => true));
  97. while (!$stream->feof()) {
  98. echo $stream->readLine();
  99. }