简体   繁体   English

如何在新的Google Calendar v3 API结果上使用PHP的“ json_decode”?

[英]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 . 我的脚本中已经准备好了所有其他内容,包括解析json_decode的数据。 The problem is that $feed doesn't contain any data. 问题在于$feed不包含任何数据。

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. 但是,我已经用AJAX解析的JavaScript实现测试了$gcal_path变量/链接(例如xmlhttp.open("GET","<?php echo $gcal_path ?>",true);正确的JSON数据。

So, why is the PHP variable empty? 那么,为什么PHP变量为空? What am I doing wrong? 我究竟做错了什么?

Extra info: Also, in the results I noticed this "etag": "\\"1576214503014905\\"" (the number has been changed for security purposes). 额外信息:另外,在结果中,我注意到了这个"etag": "\\"1576214503014905\\"" (出于安全目的,此数字已更改)。

How to deal with those escaped quotes, and would it inevitably effect the outcome of the json_decode function call, and $feed ? 如何处理那些转义的引号,是否会不可避免地影响json_decode函数调用和$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! 感谢@SGC为我指出正确的方向!

You're very much appreciated! 非常感谢您!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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