How get json Respone from Guzzle, post protected google spread sheet

吃可爱长大的小学妹 提交于 2019-12-25 04:24:48

问题


How to get response when I post request with Guzzle I use "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;

Now $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

I try $contents = (string) $postCell->getBody(); but have my request, not response, but I need response

But in docementation says about json response , where is ?

Google documentation:

These requests will return an AddNamedRangeResponse and an AddProtectedRangeResponse, containing the range IDs and properties. Like this:

{
 "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
         }
       }
     }
   }
  ]
}

I try standard:

        $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);

and this work fine I have google response in $responseData

Where find this AddProtectedRangeResponse ? In google playg ground everythins ok, Ihave resonse and I need response in my code. How to get response in guzzle ?

来源:https://stackoverflow.com/questions/38742294/how-get-json-respone-from-guzzle-post-protected-google-spread-sheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!