如何从Guzzle获得json Respone,发布受保护的谷歌电子表格

时间:2016-08-03 11:28:41

标签: php google-spreadsheet-api google-api-php-client google-api-client guzzle

当我使用Guzzle发布请求时如何获得响应我使用"guzzle/guzzle": "^3.9",

        $guzzle = new Client();

    $postCell = $guzzle
        ->post('https://sheets.googleapis.com/v4/spreadsheets/' . $spreadsheetId . ':batchUpdate',
            [],
            $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();但是有我的请求,而不是回复,但我需要回复

但是在docementation中说json响应,在哪里?

Google文档:

这些请求将返回AddNamedRangeResponse和AddProtectedRangeResponse,其中包含范围ID和属性。像这样:

{
 "spreadsheetId": "1Im64phYYa-7Xv6h_lZSUfzT2WtKuH-aK73pGM6AqnJE", 
 "replies": [
   {
     "addProtectedRange": {
       "protectedRange": {
         "requestingUserCanEdit": true, 
         "description": "Protecting total row", 
         "warningOnly": true, 
         "editors": {}, 
         "protectedRangeId": 1608527699, 
         "range": {
           "endRowIndex": 5, 
           "startRowIndex": 4, 
           "sheetId": 1789894473, 
           "startColumnIndex": 0, 
           "endColumnIndex": 5
         }
       }
     }
   }
  ]
}

我尝试标准:

        $url = 'https://sheets.googleapis.com/v4/spreadsheets/' . $spreadsheetId . ':batchUpdate';
    $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);

这项工作很好我在$responseData

中有google回复

在哪里找到这个AddProtectedRangeResponse?在谷歌playg地面,每个人都好,我有共鸣,我需要在我的代码中回应。如何在guzzle得到回应?

0 个答案:

没有答案