Retrieve the whole XML response body with Guzzle 6 HTTP Client

luqo33 picture luqo33 · Aug 3, 2015 · Viewed 21.9k times · Source

I wanted to use Guzzle 6 to retrieve an xml response from a remote API. This is my code:

$client = new Client([
    'base_uri' => '<my-data-endpoint>',
]);
$response = $client->get('<URI>', [
    'query' => [
        'token' => '<my-token>',
    ],
    'headers' => [
        'Accept' => 'application/xml'
    ]
]);
$body = $response->getBody();

Vardumping the $body would return a GuzzleHttp\Psr7\Stream object:

object(GuzzleHttp\Psr7\Stream)[453] 
private 'stream' => resource(6, stream)
...
...

I could then call $body->read(1024) to read 1024 bytes from the response (which would read in xml).

However, I'd like to retrieve the whole XML response from my request since I will need to parse it later using the SimpleXML extension.

How can I best retrieve the XML response from GuzzleHttp\Psr7\Stream object so that it is usable for parsing?

Would the while loop the way to go?

while($body->read(1024)) {
    ...
}

I'd appreciate your advice.

Answer

hakre picture hakre · Aug 3, 2015

The GuzzleHttp\Psr7\Stream implemtents the contract of Psr\Http\Message\StreamInterface which has the following to offer to you:

/** @var $body GuzzleHttp\Psr7\Stream */
$contents = (string) $body;

Casting the object to string will call the underlying __toString() method which is part of the interface. The method name __toString() is special in PHP.

As the implementation within GuzzleHttp "missed" to provide access to the actual stream handle, so you can't make use of PHP's stream functions which allows more "stream-lined" (stream-like) operations under circumstances, like stream_copy_to_stream, stream_get_contents or file_put_contents. This might not be obvious on first sight.