Retrieve the whole XML response body with Guzzle 6

2019-01-25 08:39发布

问题:

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.

回答1:

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.



回答2:

I made it this way:

public function execute ($url, $method, $headers) {
    $client = new GuzzleHttpConnection();
    $response = $client->execute($url, $method, $headers);

    return $this->parseResponse($response);
}

protected function parseResponse ($response) {
    return new SimpleXMLElement($response->getBody()->getContents());
}

My application returns content in string with XML prepared content, and Guzzle request sends headers with accept param application/xml.



回答3:

$client = new \GuzzleHttp\Client();
$response = $client->request('GET', $request_url, [
    'headers' => ['Accept' => 'application/xml'],
    'timeout' => 120
])->getBody()->getContents();

$responseXml = simplexml_load_string($response);
if ($responseXml instanceof \SimpleXMLElement)
{
    $key_value = (string)$responseXml->key_name;
}


回答4:

$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'your URL');
$response = $response->getBody()->getContents();
return $response;