Pat*_*ick 3 google-api symfony google-api-php-client
我正在使用google api客户端获取博客文章列表(请参见以下代码)。
try {
// create service and get data
$blogger = new \Google_Service_Blogger($this->client);
// https://developers.google.com/blogger/docs/3.0/reference/posts/list
// GET https://www.googleapis.com/blogger/v3/blogs/blogId/posts
if ($this->cache->contains('posts')) {
$posts = $this->cache->fetch('posts');
}
else {
$posts = $blogger->posts->listPosts($this->bloggerId, $optParams);
$this->cache->save('posts', $posts, 600); // 600 = 10 mins
}
return $posts;
} catch (apiServiceException $e) {
// Error from the API.
//print 'There was an API error : ' . $e->getCode() . ' : ' . $e->getMessage();
} catch (Exception $e) {
//print 'There was a general error : ' . $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,这都可以正常工作,但偶尔会遇到未捕获的API错误。
Google_Service_Exception: Error calling GET https://www.googleapis.com/blogger/v3/blogs/8894809254783984374/posts?maxResults=5: (500) Backend Error at /var/www/releases/20140607051057/vendor/google/apiclient/src/Google/Http/REST.php:79)"} []
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么会引发此错误,以及我该如何处理?
干杯
小智 6
问题出在命名空间上。您可以使用通用的:
} catch (\Exception $e) {
//print 'There was a general error : ' . $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)
或更具体而言:
try {
...
} catch (\Google_Service_Exception $e) {
//print 'There was a general error : ' . $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)
Exception是所有Exception的基类。参见http://php.net/manual/en/class.exception.php