Search code examples
phpjsonlaravelgoogle-apigmail-api

json_decode can't parse Gmail API json in PHP Laravel


Help me please to decode and get access_token. Have already checked 30+ stackoverflow posts about json_decode

So first is Curl. These params do not affect json response and decoding: curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'); curl_setopt($ch, CURLOPT_VERBOSE, 1); Header output shows it is in utf8 response.

  1. Checked json online enter image description here

It has no "&" or backslashes

  1. $jsonData = curl_exec($ch); and then var_dump($jsonData)-says that Google returned boolean

Here is dd result in Laravel:

enter image description here

The code after Curl

 .... 
$jsonData = curl_exec($ch);
if ($jsonData === null && json_last_error() !== JSON_ERROR_NONE) {
echo "json data is incorrect";
}

dd(
var_dump($jsonData),// tell me it is "bool(true)", after json end}
'json_decode results:',
json_decode($jsonData),
json_decode($jsonData,1),
json_decode(  ( stripslashes($jsonData) ),1),
json_decode( trim ( stripslashes($jsonData) ),1),
json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $jsonData), 0 ),
json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $jsonData), 1 )
);

Also json_decode( trim($jsonData) ); not helped


Solution

  • // tell me it is "bool(true)", after json end

    You have not even decoded anything yet, this is referring to what curl_exec actually returned here.

    You forgot to set CURLOPT_RETURNTRANSFER to true in your cURL request options.