guzzle 6.0调用未定义的方法GuzzleHttp \ Psr7 \ Response :: xml() [英] guzzle 6.0 call to undefined method GuzzleHttp\Psr7\Response::xml()

查看:888
本文介绍了guzzle 6.0调用未定义的方法GuzzleHttp \ Psr7 \ Response :: xml()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查来自服务器的基于xml的响应,这是响应格式的示例.

I want to check the xml based responses from server, here is an example of the response format.

<response>
    <code>success</code>
</response>

我现有的代码,

使用GuzzleHttp \ Client;

$client = new Client();
$response = $client->post('http://example.com/verify', [
    'form_params' => [
        'transID' => 1234,
        'orderID' => 6789,
        'token' => '0X45FJH79GD3332'
    ]
]);

$xml = $response->xml();

dd($xml);

但是,当我向服务器发出请求时,发生如下错误.

However, when I make request to the server error occurs like below.

调用未定义的方法GuzzleHttp \ Psr7 \ Response :: xml()

推荐答案

我认为文档已过时(实际上,对于5.3版,我想您使用的是6.*)

I believe the documentation is outdated (for version 5.3 actually, I suppose you're using 6.*)

他们说发送请求将返回Guzzle \ Http \ Message \ Response对象.在此版本的Guzzle中,您获得的是 GuzzleHttp \ Psr7 \ Response 它不实现xml()方法.

They say Sending a request will return a Guzzle\Http\Message\Response object. In this version of Guzzle, you're getting GuzzleHttp\Psr7\Response instead which does not implement xml() method.

您可以在 https:/中检查旧版本/github.com/guzzle/guzzle/blob/5.3/src/Message/Response.php 并使用该方法.例如.创建此:

You can go and check old version at https://github.com/guzzle/guzzle/blob/5.3/src/Message/Response.php and use that method. Eg. create this:

public function xml(Request $request, array $config = [])
{
    $disableEntities = libxml_disable_entity_loader(true);
    $internalErrors = libxml_use_internal_errors(true);
    try {
        // Allow XML to be retrieved even if there is no response body
        $xml = new \SimpleXMLElement(
            (string) $request->getBody() ?: '<root />',
            isset($config['libxml_options']) ? $config['libxml_options'] : LIBXML_NONET,
            false,
            isset($config['ns']) ? $config['ns'] : '',
            isset($config['ns_is_prefix']) ? $config['ns_is_prefix'] : false
        );
        libxml_disable_entity_loader($disableEntities);
        libxml_use_internal_errors($internalErrors);
    } catch (\Exception $e) {
        libxml_disable_entity_loader($disableEntities);
        libxml_use_internal_errors($internalErrors);
        throw new YourXmlParseException(
            'Unable to parse response body into XML: ' . $e->getMessage(),
            $request,
            $e,
            (libxml_get_last_error()) ?: null
        );
    }
    return $xml;
}

这篇关于guzzle 6.0调用未定义的方法GuzzleHttp \ Psr7 \ Response :: xml()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆