如何使用PHP';s';json_decode';关于新的Google Calendar v3 API


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

这是我的代码:

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

我已经在脚本中准备好了所有其他内容,包括解析json_decode中的数据。问题是$feed不包含任何数据。

然而,我已经用AJAX解析的JavaScript实现测试了$gcal_path变量/链接(例如xmlhttp.open("GET","<?php echo $gcal_path ?>",true);。它会吐出所有正确的JSON数据。

那么,为什么PHP变量是空的呢?我做错了什么?

额外信息:此外,在结果中,我注意到这个"etag": "'"1576214503014905'""(为了安全起见,数字已经更改)。

如何处理这些转义引号,它会不可避免地影响json_decode函数调用和$feed的结果吗?

请帮帮我。

已解决!

这是工作代码:

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

感谢@SGC为我指明了正确的方向!

非常感谢您!