PHP Google 日历 API 未返回错误字符串
PHP Google Calendar API not returning error string
我正在使用一个日历,它可以自动同步并根据请求(如果用户指定)与 G!Calendar 同步。
一切正常,除了我需要检查用户提供的日历 ID 是否存在。
为此我使用:
$cal = "GOOGLE CALENDAR ID";
$service = new Google_Service_Calendar($client);
$title = $service->calendars->get($cal);
如果ID存在,脚本运行正常。如果没有,则页面崩溃。
我怎样才能得到它应该 return 的错误 JSON 字符串(如 API 资源页面中指定的那样),以便我可以警告用户并阻止这些错误传播?
提前致谢。
你应该做一个 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();
}
}
我正在使用一个日历,它可以自动同步并根据请求(如果用户指定)与 G!Calendar 同步。 一切正常,除了我需要检查用户提供的日历 ID 是否存在。
为此我使用:
$cal = "GOOGLE CALENDAR ID";
$service = new Google_Service_Calendar($client);
$title = $service->calendars->get($cal);
如果ID存在,脚本运行正常。如果没有,则页面崩溃。 我怎样才能得到它应该 return 的错误 JSON 字符串(如 API 资源页面中指定的那样),以便我可以警告用户并阻止这些错误传播?
提前致谢。
你应该做一个 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();
}
}