简体   繁体   中英

How to use PHP's 'json_decode' on the new Google Calendar v3 API results?

Here is my code:

$gcal_path = "https://www.googleapis.com/calendar/v3/calendars/".$gcal_url_encoded_id."/events?maxResults=".$max_Results."&orderBy=startTime&singleEvents=true&timeMax=".$time_Max."&timeMin=".$time_Min."&key=".$api_key;

$feed = json_decode(html_entity_decode(trim(file_get_contents($gcal_path))));

I already have everything else ready in my script, including parsing the data from the json_decode . The problem is that $feed doesn't contain any data.

However, I've tested the $gcal_path variable/link with a JavaScript implementation of parsing with AJAX ( eg xmlhttp.open("GET","<?php echo $gcal_path ?>",true); . and it spits out all the right JSON data.

So, why is the PHP variable empty? What am I doing wrong?

Extra info: Also, in the results I noticed this "etag": "\\"1576214503014905\\"" (the number has been changed for security purposes).

How to deal with those escaped quotes, and would it inevitably effect the outcome of the json_decode function call, and $feed ?

Please, help.

SOLVED!

Here's the working code:

$gcal_path = "https://www.googleapis.com/calendar/v3/calendars/".$gcal_url_encoded_id."/events?maxResults=".$max_Results."&orderBy=startTime&singleEvents=true&timeMax=".$time_Max."&timeMin=".$time_Min."&key=".$api_key;

// Get cURL resource.
$curl = curl_init();

// Set some options - we are passing in a user-agent too here.
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $gcal_path,
    CURLOPT_USERAGENT => '{enter a user-agent string ( some can be found at: http://www.useragentstring.com/pages/Chrome/) here, without the curly braces} ',
    CURLOPT_REFERER => '{enter the referrer URL, that is allowed to get the calendar JSON, here, without the curly braces}'
));
// Send the request & save response to $resp.
$resp = curl_exec($curl);
// Close request to clear up some resources.
curl_close($curl);

// Populate the '$feed' variable with decoded JSON data.
$feed = json_decode($resp); 

Thanks @SGC for pointing me in the right direction!

You're very much appreciated!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM