ZF2 Http请求问题

时间:2017-01-17 12:33:25

标签: zend-framework2

我想知道我是否遗漏了什么。

我的第一个代码:

    $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] =>排列           (               [100] =>继续               [101] =>切换协议               [102] =>处理               [200] =>好               [201] =>创建               [202] =>公认               [203] =>非权威信息               [204] =>无内容               [205] =>重置内容               [206] =>部分内容               [207] =>多状态               [208] =>已经报道过了               [300] =>多种选择               [301] =>永久移动               [302] =>发现               [303] =>见其他               [304] =>没有修改               [305] =>使用代理服务器               [306] =>切换代理               [307] =>临时重定向               [400] =>错误的请求               [401] =>擅自               [402] =>需要付款               [403] =>被禁止               [404] =>未找到               [405] =>方法不允许               [406] =>不能接受的               [407] =>需要代理验证               [408] =>请求超时               [409] =>冲突               [410] =>飘               [411] =>长度要求               [412] =>前提条件失败               [413] =>请求的实体太大               [414] =>请求URI太大               [415] =>不支持的媒体类型               [416] =>要求的范围不能令人满意               [417] =>期望失败               [418] =>我是一个茶壶               [422] =>不可处理的实体               [423] =>锁定               [424] =>失败的依赖               [425] =>无序集合               [426] =>需要升级               [428] =>前提要求               [429] =>请求太多               [431] =>请求标头字段太大               [500] =>内部服务器错误               [501] =>未实现               [502] =>错误的网关               [503] =>暂停服务               [504] =>网关超时               [505] =>不支持HTTP版本               [506] =>变种也谈判               [507] =>存储空间不足               [508] =>检测到环路               [511] =>需要网络验证           )

[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

1 个答案:

答案 0 :(得分:1)

这不是一个错误,它是一个Zend \ Http \ Response对象,当你调用send方法时由Client对象返回

你可以从你发布的输出中看到它成功返回(Http 200 Response)。

[statusCode:protected] => 200

并返回了一些压缩内容

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

你只需要打电话:

$response->getBody();

将返回响应的解码内容主体

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