【发布时间】:2012-02-15 17:08:22
【问题描述】:
我如何知道我的访问令牌是否已过期。
我正在使用 try and catch
try {
$result = file_get_contents('https://www.googleapis.com/calendar/v3/calendars/primary/events?access_token='.$accesstoken);
print_r($result);
}
catch(Exception $e){
echo "Get new token";
}
但它仍然从 file_get_contents 得到错误,然后打印“获取新令牌”
如果我想使用 curl
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'https://www.googleapis.com/calendar/v3/calendars/primary/events?access_token='.$accesstoken);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
那么我应该怎么做才能捕捉到这里的错误?
//if error because access token is invalid,
do here
// my fixed solution
$response= json_decode($result);
if($response->error){ // if result has errors
echo "Get new token";
}
【问题讨论】:
-
您的问题是什么?你想知道什么时候要求新的访问令牌或如何要求新的访问令牌?
-
@shadow 是的,我想知道我的访问令牌是否已过期。我已经用我的解决方案编辑了我的帖子。请随时提出任何建议。
标签: php curl google-api access-token