谷歌日历错误处理

时间:2012-11-20 16:05:29

标签: php google-calendar-api

我正在使用谷歌日历API的V3与基于php的应用程序中的日历同步。

我使用此代码删除Google日历上的活动。

$delJson = json_decode($service->events->delete('primary', $gcal_id), true);

如果找到Google日历ID,一切都会顺利进行。 但是,如果在$ gcal_id和实际的Google日历ID之间找不到匹配项,则整个过程会停止并显示空白页。

至少,我希望这个过程能够继续而不会完全停止。 我想代码在逻辑上会是这样的,但我找不到任何文档。

$delJson = json_decode($service->events->delete('primary', $gcal_id), true);

if ($response == error) { continue } 

1 个答案:

答案 0 :(得分:2)

我很惊讶这里没有得到回答,或者其他任何地方都没有得到回答;我遇到了同样的问题,看到它被多次询问而没有多少帮助回答它。

V3 PHP api使用异常,你需要处理的异常可能与update()相同,使用类似于下面的Google_Service_Exception:

try {
    $cal->events->update($this->calName,$e,$event);
} catch (Google_Service_Exception $e ) {
    echo "Caught Google_Service_Exception:";
    print_r($e);
}

注意,如果你有一个get,update,你需要尝试get和update。 API中还有其他几种异常类型,如果不处理,可能会导致您的网站访问者出现问题。 Google_Auth_Exception,Google_IO_Exception,Google_Cache_Exception。因此,除非您已经研究了特定情况以确保处理各种可能的错误,否则最好只是在Google API代码周围放置一个通用异常捕获器。