如何在Guzzle获得反应体

时间:2016-08-04 07:05:28

标签: php guzzle

当我使用Guzzle发布请求时如何获得响应我使用<div class="modal-header"> <h1>This is the title {{item.name}}</h1> </div>' <div ng-controller="Nav" class="modal-body"> </div> <div class="modal-footer"> <button class="btn btn-primary" ng-click="ok()">OK</button> <button class="btn btn-warning" ng-click="cancel()">Cancel</button> </div> 我发布了一些url请求,我需要响应正文,但是Guzzle我没有发现这是响应,使用标准的php函数"guzzle/guzzle": "^3.9",我有响应体。如何在Guzzle het响应身体?

file_get_contents

现在$guzzle = new Client(); $postCell = $guzzle ->post($url, [], $addProtectedRangeJson) ->addHeader('Authorization', 'Bearer ' . $arrayAccessTokenClient) ->addHeader('Content-type', 'application/json') ; $postCell ->send() ->getBody(true) ; $contents = (string) $postCell->getBody(); // get body that I post -> my request, not response $contents = $postCell->getBody()->getContents(); // not find getContents, ask me did you mean to call getContentMd5 $answer = json_decode($postCell->getResponse()->getBody(), true); return $answer;

$answer

我尝试result = {Guzzle\Http\EntityBody} [7] readWriteHash = {array} [2] contentEncoding = false rewindFunction = null stream = {resource} resource id='77' type='stream' size = null cache = {array} [9] wrapper_type = "PHP" stream_type = "TEMP" mode = "w+b" unread_bytes = 0 seekable = true uri = "php://temp" is_local = true is_readable = true is_writable = true customData = {array} [1] default = true 但是有我的请求,而不是回复,但我需要回复

$contents = (string) $postCell->getBody();

如何在Guzzle中获得响应体?

我试试http guzzle

        $url = 'some_url';
    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'=>"Content-type: application/json\r\n" .
                "Authorization: Bearer $arrayAccessTokenClient\r\n",
            'content' => $addProtectedRangeJson
        )
    );
    // Creating the context for the request
    $context = stream_context_create($opts);
    $response = file_get_contents($url, FALSE, $context);
    $responseData = json_decode($response, TRUE);

但是有401吗?热添加标题?

更新

$headers = [
        'Authorization' => 'Bearer ' . $arrayAccessTokenClient,
        'Content-type' => 'application/json'
    ];
    $guzzle = new \GuzzleHttp\Client($headers);

    $request = $guzzle
        ->post('https://sheets.googleapis.com/v4/spreadsheets/' . $spreadsheetId . ':batchUpdate',
            $addProtectedRange
        );
    $response = $request->getBody()->getContents();

    return $response;

但有

    $headers = [
        'Authorization' => 'Bearer ' . $arrayAccessTokenClient,
        'Content-type' => 'application/json'
    ];
    $guzzle = new \GuzzleHttp\Client();
    $request = new Request('POST', 'https://sheets.googleapis.com/v4/spreadsheets/' . $spreadsheetId . ':batchUpdate', $headers);
    $response = $guzzle
        ->send($request, $addProtectedRange);

    $answer = $response->getBody()->getContents();

    return $answer;

2 个答案:

答案 0 :(得分:3)

我找到解决方案

$headers = [
    'Authorization' => 'Bearer ' . $arrayAccessTokenClient,
    'Content-type' => 'application/json'
];
$guzzle = new \GuzzleHttp\Client();
$request = new \GuzzleHttp\Psr7\Request('POST', 'https://sheets.googleapis.com/v4/spreadsheets/' . $spreadsheetId . ':batchUpdate', $headers);
$response = $guzzle
    ->send($request, ['body' => $addProtectedRangeJson]);

$answer = $response->getBody()->getContents();

return $answer;

答案 1 :(得分:0)

To retrieve all the data, you can use casting operator:

$contents = (string) $response->getBody();

You can also do it with

$contents = $response->getBody()->getContents();

The difference between the two approaches is that getContents returns the remaining contents, so that a second call returns nothing unless you seek the position of the stream with rewind or seek .

$stream = $response->getBody();
$contents = $stream->getContents(); // returns all the contents
$contents = $stream->getContents(); // empty string
$stream->rewind(); // Seek to the beginning
$contents = $stream->getContents(); // returns all the contents
Instead, usings PHP's string casting operations, it will reads all the data from the stream from the beginning until the end is reached.

$contents = (string) $response->getBody(); // returns all the contents
$contents = (string) $response->getBody(); // returns all the contents
相关问题