如何使用PHP' json_decode'在新的Google Calendar v3 API结果中?

时间:2015-03-19 20:00:42

标签: javascript php json google-api google-calendar-api

这是我的代码:

$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

请帮助。

1 个答案:

答案 0 :(得分:1)

解决!

这是工作代码:

$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指出我正确的方向!

非常感谢你!