Laravel 5 guzzle 6.0调用未定义的方法guzzle http\Psr7\Response::xml()
我想检查来自服务器的基于xml的响应,下面是响应格式的一个示例Laravel 5 guzzle 6.0调用未定义的方法guzzle http\Psr7\Response::xml(),laravel-5,guzzle6,Laravel 5,Guzzle6,我想检查来自服务器的基于xml的响应,下面是响应格式的一个示例 <response> <code>success</code> </response> 我现有的代码 使用http\Client $client = new Client(); $response = $client->post('http://example.com/verify', [ 'form_params' => [ 'trans
<response>
<code>success</code>
</response>
我现有的代码
使用http\Client强>
$client = new Client();
$response = $client->post('http://example.com/verify', [
'form_params' => [
'transID' => 1234,
'orderID' => 6789,
'token' => '0X45FJH79GD3332'
]
]);
$xml = $response->xml();
dd($xml);
但是,当我向服务器发出请求时,会出现如下错误
调用未定义的方法GuzzleHttp\Psr7\Response::xml()我认为文档已经过时(对于5.3版,我想您使用的是6.*) 他们说发送请求将返回一个Guzzle\Http\Message\Response对象。在这个版本的Guzzle中,您得到的是GuzzleHttp\Psr7\Response,它没有实现
xml()
方法
您可以在上检查旧版本并使用该方法。例如,创建这个:
公共函数xml(请求$Request,数组$config=[])
{
$disableEntities=libxml\u disable\u entity\u loader(true);
$internalErrors=libxml\u use\u internal\u errors(true);
试一试{
//即使没有响应主体,也允许检索XML
$xml=new\simplexmlement(
(字符串)$request->getBody()?:“”,
isset($config['libxml\u options'])?$config['libxml\u options']:libxml\u NONET,
假,,
isset($config['ns'])?$config['ns']:“”,
isset($config['ns\u是\u前缀])?$config['ns\u是\u前缀]:false
);
libxml_disable_entity_loader($disableEntities);
libxml\u使用内部错误($internalErrors);
}捕获(\异常$e){
libxml_disable_entity_loader($disableEntities);
libxml\u使用内部错误($internalErrors);
抛出新的XmlParseException(
“无法将响应正文解析为XML:”。$e->getMessage(),
$request,
$e,
(libxml_get_last_error())?:null
);
}
返回$xml;
}
你好。我也有同样的问题。我从guzzle得到一个XML响应,应该能够访问SimpleXML对象中的值。现在guzzle 6不支持我尝试使用旧版本的函数。但我不知道如何实际使用它。我必须延长口吻吗?或者,如何从中获得适当的XML对象@迪潘德拉·古隆(),你是怎么做到的?该死,我没能恰当地提到你,迪潘德拉·古隆。通常,每当我键入@dipe…,它都会自动提示。。。。对吗?@Merc我稍微修改了我的答案。只需在代码中使用此函数并传递响应。(没有测试它,只是从5.3代码中修改了fcn)我找到了另一个解决方案,它可能没有这样健壮,但实现了以下技巧:$body=$response->getBody()$xml=simplexml\u load\u字符串($body)代码>