如何知道我的訪問令牌是否已過期。如何知道訪問令牌是否在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是的,我想知道我的訪問令牌是否已過期。我用我的解決方案編輯了我的帖子。請隨時提出任何建議。 –