PHP Google Calendar API不返回错误字符串

时间:2018-03-20 11:02:56

标签: php google-api google-api-php-client google-calendar-api

我使用的日历会根据请求自动同步(如果用户指定)与G!日历同步。 一切正常,但我需要检查用户提供的日历ID是否存在。

为此,我使用:

$cal = "GOOGLE CALENDAR ID";
$service = new Google_Service_Calendar($client);
$title = $service->calendars->get($cal);

如果ID存在,脚本运行正常。如果它没有,那么页面就会崩溃。 如何获取它应该返回的错误JSON字符串(如API资源页面中指定的那样),因此我可以警告用户并阻止这些错误传播?

提前致谢。

1 个答案:

答案 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();
  }
}
相关问题