我使用的日历会根据请求自动同步(如果用户指定)与G!日历同步。 一切正常,但我需要检查用户提供的日历ID是否存在。
为此,我使用:
$cal = "GOOGLE CALENDAR ID";
$service = new Google_Service_Calendar($client);
$title = $service->calendars->get($cal);
如果ID存在,脚本运行正常。如果它没有,那么页面就会崩溃。 如何获取它应该返回的错误JSON字符串(如API资源页面中指定的那样),因此我可以警告用户并阻止这些错误传播?
提前致谢。
答案 0 :(得分:0)
您应该执行try catch错误将被捕获,您可以向用户显示该消息。
function printCalendar($service, $fileId) {
try {
$calendar = $service->calendars->get($cal);
print "Title: " . $file->getTitle();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}