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 GuzzleHttpPsr7Stream
object:
object(GuzzleHttpPsr7Stream)[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 GuzzleHttpPsr7Stream
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…