使用 Guzzle 6 HTTP 客户端检索整个 XML 响应正文
Retrieve the whole XML response body with Guzzle 6 HTTP Client
我想使用 Guzzle 6 从远程 API 检索 xml 响应。这是我的代码:
$client = new Client([
'base_uri' => '<my-data-endpoint>',
]);
$response = $client->get('<URI>', [
'query' => [
'token' => '<my-token>',
],
'headers' => [
'Accept' => 'application/xml'
]
]);
$body = $response->getBody();
Vardumping $body
会 return 一个 GuzzleHttp\Psr7\Stream
对象:
object(GuzzleHttp\Psr7\Stream)[453]
private 'stream' => resource(6, stream)
...
...
然后我可以调用 $body->read(1024)
从响应中读取 1024 个字节(将读入 xml)。
但是,我想从我的请求中检索整个 XML 响应,因为稍后我需要使用 SimpleXML
扩展来解析它。
如何才能最好地从 GuzzleHttp\Psr7\Stream
对象检索 XML 响应,以便它可用于解析?
while
会循环吗?
while($body->read(1024)) {
...
}
非常感谢您的建议。
GuzzleHttp\Psr7\Stream implemtents the contract of Psr\Http\Message\StreamInterface有以下内容提供给您:
/** @var $body GuzzleHttp\Psr7\Stream */
$contents = (string) $body;
将对象转换为字符串将调用作为接口一部分的底层 __toString()
方法。 method name __toString()
is special in PHP.
由于 GuzzleHttp "missed" 中的实现提供对实际流句柄的访问,因此您不能使用 PHP 的流在 stream_copy_to_stream
、stream_get_contents
或 file_put_contents
等情况下允许更多 "stream-lined"(stream-like)操作的函数。乍一看这可能并不明显。
我是这样做的:
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());
}
我的应用程序 returns 字符串中的内容具有 XML 准备好的内容,并且 Guzzle 请求发送 headers 并接受参数 application/xml 。
$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;
}
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'your URL');
$response = $response->getBody()->getContents();
return $response;
我想使用 Guzzle 6 从远程 API 检索 xml 响应。这是我的代码:
$client = new Client([
'base_uri' => '<my-data-endpoint>',
]);
$response = $client->get('<URI>', [
'query' => [
'token' => '<my-token>',
],
'headers' => [
'Accept' => 'application/xml'
]
]);
$body = $response->getBody();
Vardumping $body
会 return 一个 GuzzleHttp\Psr7\Stream
对象:
object(GuzzleHttp\Psr7\Stream)[453]
private 'stream' => resource(6, stream)
...
...
然后我可以调用 $body->read(1024)
从响应中读取 1024 个字节(将读入 xml)。
但是,我想从我的请求中检索整个 XML 响应,因为稍后我需要使用 SimpleXML
扩展来解析它。
如何才能最好地从 GuzzleHttp\Psr7\Stream
对象检索 XML 响应,以便它可用于解析?
while
会循环吗?
while($body->read(1024)) {
...
}
非常感谢您的建议。
GuzzleHttp\Psr7\Stream implemtents the contract of Psr\Http\Message\StreamInterface有以下内容提供给您:
/** @var $body GuzzleHttp\Psr7\Stream */
$contents = (string) $body;
将对象转换为字符串将调用作为接口一部分的底层 __toString()
方法。 method name __toString()
is special in PHP.
由于 GuzzleHttp "missed" 中的实现提供对实际流句柄的访问,因此您不能使用 PHP 的流在 stream_copy_to_stream
、stream_get_contents
或 file_put_contents
等情况下允许更多 "stream-lined"(stream-like)操作的函数。乍一看这可能并不明显。
我是这样做的:
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());
}
我的应用程序 returns 字符串中的内容具有 XML 准备好的内容,并且 Guzzle 请求发送 headers 并接受参数 application/xml 。
$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;
}
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'your URL');
$response = $response->getBody()->getContents();
return $response;