将cURL请求转换为Guzzle请求时缺少响应数据

时间:2016-09-26 14:41:21

标签: php curl guzzle guzzle6

我正在尝试使用API连接到Guzzle服务。不久之前,我设法让请求和响应工作,我现在正尝试使用Guzzle做同样的事情。

我对cURL所拥有的是:

    $post_data = array('test' => 'test data' ...);

    $url = 'https://website/api/v1/objects.json';
    $ch = curl_init($url);
    curl_setopt_array(
        $ch,
        array(
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => array(
                'Authorization: Basic xxxxxxxxx',
                'Content-Type: application/json',
                'X-Environment: sandbox'
            ),
            CURLOPT_POSTFIELDS => json_encode($post_data)
        )
    );
    $json_response = curl_exec($ch);
    $response = json_decode($json_response, true);
    print_r($response);

在Guzzle,到目前为止,我做到了这一点:

$headers = array(
    'Authorization' => 'Basic xxx',
    'Content-Type' => 'application/json',
    'X-Environment' => 'sandbox'
    );

$post_data = array('test' => 'test data' ...);
$client = new GuzzleHttp\Client();

$response = '';
try {
    $response = $client->request('PUT', 'https://website/api/v1/object.json',
            [
                'headers' => $headers,
                'json' => $post_data,
            ]);

    } catch (RequestException $e) {
        echo Psr7\str($e->getRequest());
        if ($e->hasResponse()) {
            echo Psr7\str($e->getResponse());
        }
    }
    print_r($response);
    echo $response->getStatusCode();
    echo $response->getBody();

我从cURL请求得到的回复是:

Array
(
    [timestamp] => 2016-09-26T14:36:14+0000
    [message] => Array
        (
            [status] => success
            [type] => imported
            [code] => imported-000
            [text] => Your request was validated successfully
        )

    [sandbox] => Array
        (
            [imported] => Callback successfully sent: 200
            [produced] => Callback successfully sent: 200
        )

)

这就是我所期待的,但是当我使用Guzzle时,我得到了这个:

GuzzleHttp\Psr7\Response Object
(
    [reasonPhrase:GuzzleHttp\Psr7\Response:private] => OK
    [statusCode:GuzzleHttp\Psr7\Response:private] => 200
    [headers:GuzzleHttp\Psr7\Response:private] => Array
        (
            [Date] => Array
                (
                    [0] => Mon, 26 Sep 2016 14:22:54 GMT
                )

            [Content-Type] => Array
                (
                    [0] => text/html; charset=utf-8
                )

            [Transfer-Encoding] => Array
                (
                    [0] => chunked
                )

            [Set-Cookie] => Array
                (
                    [0] => Session=t3onh2ak87q46qq2sn1fm0s6u0; path=/
                )

            [Expires] => Array
                (
                    [0] => Thu, 19 Nov 1981 08:52:00 GMT
                )

            [Cache-Control] => Array
                (
                    [0] => no-store, no-cache, must-revalidate
                )

            [Pragma] => Array
                (
                    [0] => no-cache
                )

            [Access-Control-Allow-Origin] => Array
                (
                    [0] => *
                )

            [X-Robots-Tag] => Array
                (
                    [0] => noindex
                )

        )

    [headerNames:GuzzleHttp\Psr7\Response:private] => Array
        (
            [date] => Date
            [content-type] => Content-Type
            [transfer-encoding] => Transfer-Encoding
            [set-cookie] => Set-Cookie
            [expires] => Expires
            [cache-control] => Cache-Control
            [pragma] => Pragma
            [access-control-allow-origin] => Access-Control-Allow-Origin
            [x-robots-tag] => X-Robots-Tag
        )

    [protocol:GuzzleHttp\Psr7\Response:private] => 1.1
    [stream:GuzzleHttp\Psr7\Response:private] => GuzzleHttp\Psr7\Stream Object
        (
            [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #313
            [size:GuzzleHttp\Psr7\Stream:private] => 
            [seekable:GuzzleHttp\Psr7\Stream:private] => 1
            [readable:GuzzleHttp\Psr7\Stream:private] => 1
            [writable:GuzzleHttp\Psr7\Stream:private] => 1
            [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
            [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
                (
                )

        )

)

请求似乎成功,但我不知道如何获取我需要的数据。我在请求中做错了吗?

0 个答案:

没有答案