如何在 Guzzle 上获得 XML 响应
How to get XML response on Guzzle
我一直在尝试将正文 return 的文本解析为 XML(应该 return 在 XML 中)但到目前为止它只是 return就像文字一样。
这是代码以及应该是什么样子的。
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Psr7\Request;
$client = new GuzzleHttp\Client();
$options = [
'headers' => ['Accept'=>'application/xml'],
'auth' => ['Deversor-Channel', '?1_sIWVZ((ujV)kDBM!6O5!AFAQM*K*yr(?E.(g='],
'body' => "<request>
<hotel_id>345786
</hotel_id>
<last_change>2021-07-19</last_change>
</request>",
];
$res = $client->request('POST', 'https://secure-supply-xml.booking.com/hotels/xml/reservations', $options);
echo $res->getStatusCode();
// "200"
echo $res->getBody();
// {"type":"User"...'
$xml = simplexml_load_string($res->getBody(),'SimpleXMLElement',LIBXML_NOCDATA);
echo $xml;
This is how it should look like:
This is how it looks
您现在正在执行的 echo 将触发您使用 simplexml_load_string()
创建的 SimpleXMLElement 的 __toString()
方法,这实际上是 [=] 的字符串内容21=].
你要找的是$xml->asXML()
,它会输出一个well-formed XML字符串。您可能还需要为某些浏览器添加 Content-Type
header 以获取 XML 内容。尝试以下操作:
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Psr7\Request;
$client = new GuzzleHttp\Client();
$options = [
'headers' => ['Accept'=>'application/xml'],
'auth' => ['Deversor-Channel', '?1_sIWVZ((ujV)kDBM!6O5!AFAQM*K*yr(?E.(g='],
'body' => "<request>
<hotel_id>345786
</hotel_id>
<last_change>2021-07-19</last_change>
</request>",
];
$res = $client->request('POST', 'https://secure-supply-xml.booking.com/hotels/xml/reservations', $options);
echo $res->getStatusCode();
// "200"
echo $res->getBody();
// {"type":"User"...'
$xml = simplexml_load_string($res->getBody(),'SimpleXMLElement',LIBXML_NOCDATA);
header('Content-Type: application/xml');
echo $xml->asXML();
我一直在尝试将正文 return 的文本解析为 XML(应该 return 在 XML 中)但到目前为止它只是 return就像文字一样。 这是代码以及应该是什么样子的。
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Psr7\Request;
$client = new GuzzleHttp\Client();
$options = [
'headers' => ['Accept'=>'application/xml'],
'auth' => ['Deversor-Channel', '?1_sIWVZ((ujV)kDBM!6O5!AFAQM*K*yr(?E.(g='],
'body' => "<request>
<hotel_id>345786
</hotel_id>
<last_change>2021-07-19</last_change>
</request>",
];
$res = $client->request('POST', 'https://secure-supply-xml.booking.com/hotels/xml/reservations', $options);
echo $res->getStatusCode();
// "200"
echo $res->getBody();
// {"type":"User"...'
$xml = simplexml_load_string($res->getBody(),'SimpleXMLElement',LIBXML_NOCDATA);
echo $xml;
This is how it should look like:
This is how it looks
您现在正在执行的 echo 将触发您使用 simplexml_load_string()
创建的 SimpleXMLElement 的 __toString()
方法,这实际上是 [=] 的字符串内容21=].
你要找的是$xml->asXML()
,它会输出一个well-formed XML字符串。您可能还需要为某些浏览器添加 Content-Type
header 以获取 XML 内容。尝试以下操作:
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Psr7\Request;
$client = new GuzzleHttp\Client();
$options = [
'headers' => ['Accept'=>'application/xml'],
'auth' => ['Deversor-Channel', '?1_sIWVZ((ujV)kDBM!6O5!AFAQM*K*yr(?E.(g='],
'body' => "<request>
<hotel_id>345786
</hotel_id>
<last_change>2021-07-19</last_change>
</request>",
];
$res = $client->request('POST', 'https://secure-supply-xml.booking.com/hotels/xml/reservations', $options);
echo $res->getStatusCode();
// "200"
echo $res->getBody();
// {"type":"User"...'
$xml = simplexml_load_string($res->getBody(),'SimpleXMLElement',LIBXML_NOCDATA);
header('Content-Type: application/xml');
echo $xml->asXML();