ZF2 Http 请求问题

ZF2 Http Request Issue

我想弄清楚我是否遗漏了什么。

我的第一个代码:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlParam->asXML());
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
    $data = curl_exec($ch);
    curl_close($ch);

这工作正常,

第二个代码:

    $client = new Client($url, array(
        'maxredirects' => 0,
        'timeout'      => 30,
        'adapter' => 'Zend\Http\Client\Adapter\Curl'));
    $client->setMethod("POST");
    $client->setRawBody($xmlParam->asXML());
    $client->setEncType('text/xml');

    $response = $client->send();

这就是问题所在,

第二组代码出错

Zend\Http\Response Object ( [recommendedReasonPhrases:protected] => Array ( [100] => Continue [101] => Switching Protocols [102] => Processing [200] => OK [201] => Created [202] => Accepted [203] => Non-Authoritative Information [204] => No Content [205] => Reset Content [206] => Partial Content [207] => Multi-status [208] => Already Reported [300] => Multiple Choices [301] => Moved Permanently [302] => Found [303] => See Other [304] => Not Modified [305] => Use Proxy [306] => Switch Proxy [307] => Temporary Redirect [400] => Bad Request [401] => Unauthorized [402] => Payment Required [403] => Forbidden [404] => Not Found [405] => Method Not Allowed [406] => Not Acceptable [407] => Proxy Authentication Required [408] => Request Time-out [409] => Conflict [410] => Gone [411] => Length Required [412] => Precondition Failed [413] => Request Entity Too Large [414] => Request-URI Too Large [415] => Unsupported Media Type [416] => Requested range not satisfiable [417] => Expectation Failed [418] => I'm a teapot [422] => Unprocessable Entity [423] => Locked [424] => Failed Dependency [425] => Unordered Collection [426] => Upgrade Required [428] => Precondition Required [429] => Too Many Requests [431] => Request Header Fields Too Large [500] => Internal Server Error [501] => Not Implemented [502] => Bad Gateway [503] => Service Unavailable [504] => Gateway Time-out [505] => HTTP Version not supported [506] => Variant Also Negotiates [507] => Insufficient Storage [508] => Loop Detected [511] => Network Authentication Required )

[statusCode:protected] => 200
[reasonPhrase:protected] => OK
[version:protected] => 1.1
[headers:protected] => Zend\Http\Headers Object
    (
        [pluginClassLoader:protected] => 
        [headersKeys:protected] => Array
            (
                [0] => cachecontrol
                [1] => pragma
                [2] => contenttype
                [3] => contentencoding
                [4] => vary
                [5] => server
                [6] => xaspnetversion
                [7] => xpoweredby
                [8] => xframeoptions
                [9] => xcontenttypeoptions
                [10] => contentsecuritypolicy
                [11] => date
                [12] => connection
            )

        [headers:protected] => Array
            (
                [0] => Array
                    (
                        [name] => Cache-Control
                        [line] => Cache-Control: no-cache
                    )

                [1] => Array
                    (
                        [name] => Pragma
                        [line] => Pragma: no-cache
                    )

                [2] => Array
                    (
                        [name] => Content-Type
                        [line] => Content-Type: text/xml; charset=utf-8
                    )

                [3] => Array
                    (
                        [name] => Content-Encoding
                        [line] => Content-Encoding: gzip
                    )

                [4] => Array
                    (
                        [name] => Vary
                        [line] => Vary: Accept-Encoding
                    )

                [5] => Array
                    (
                        [name] => Server
                        [line] => Server: Microsoft-IIS/8.5
                    )

                [6] => Array
                    (
                        [name] => X-AspNet-Version
                        [line] => X-AspNet-Version: 4.0.30319
                    )

                [7] => Array
                    (
                        [name] => X-Powered-By
                        [line] => X-Powered-By: ASP.NET
                    )

                [8] => Array
                    (
                        [name] => X-Frame-Options
                        [line] => X-Frame-Options: DENY
                    )

                [9] => Array
                    (
                        [name] => X-Content-Type-Options
                        [line] => X-Content-Type-Options: nosniff
                    )

                [10] => Array
                    (
                        [name] => Content-Security-Policy
                        [line] => Content-Security-Policy: frame-ancestors 'none';
                    )

                [11] => Array
                    (
                        [name] => Date
                        [line] => Date: Tue, 17 Jan 2017 12:09:01 GMT
                    )

                [12] => Array
                    (
                        [name] => Connection
                        [line] => Connection: close
                    )

            )

    )

[metadata:protected] => Array
    (
    )

[content:protected] => ���`I�%&/m�{J�J��t��`$ؐ@������iG#)�*��eVe]f@�흼��{���{���;�N'���?\fdl��J�ɞ!���?~|?"��ez��MQ-?�hw��Q�/�լX^|�Ѻ=�>���8����uެ�e���i���e{t��՗��տ�������^�GO�z��������������'oξ|���k��/��.�ߧZ�YY���:�g�y��m�-�l�z�U���v���=M��U]]�|��

@B� ���i���

这不是错误,它是一个 Zend\Http\Response 对象,在您调用发送方法时 return 由客户端对象编辑

您可以从发布的输出中看到它 returned 成功(Http 200 响应)。

[statusCode:protected] => 200

和return编辑了一些压缩内容

[content:protected] => ���...

您只需拨打:

$response->getBody();

这将 return 响应的解码内容主体

有关详细信息,请参阅 https://framework.zend.com/manual/2.4/en/modules/zend.http.response.html