如何知道我的访问令牌是否已过期。如何知道访问令牌是否在Google Api中过期PHP
我使用try和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得到错误,则打印 “获取新的令牌”
如果我想使用卷曲
$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
@shadow是的,我想知道我的访问令牌是否已过期。我用我的解决方案编辑了我的帖子。请随时提出任何建议。 –