您可以通过异常处理来做到这一点。如果您的访问令牌已过期或无效,它将向您抛出错误并为您提供下面提到的消息。
public function getUserInfo($access_token)
{
try {
$user_info = $this->facebook->api(
'/me',
'GET',
array('access_token' =>$access_token)
);
}
catch(Exception $e){
$message = $e->getMessage();
if ((strpos($message, 'Error validating access token') !== false) ||
(strpos($message, 'Invalid OAuth access token') !== false) ||
(strpos($message, 'An active access token must be used') !== false)
) {
echo 'Your custom Message';
}
}
}
您可以将访问令牌传递给您的函数,然后像上面的异常处理那样检查。
希望它可以帮助你。
通过这种方式,您可以获得扩展访问令牌
/**
* Getting User Acess Tocken , extended it and save it in to database......
*/
public function getAccessToken($user_id,$fb_account_id)
{
$access_token=$this->facebook->getAccessToken();
$extended_access_token=$this->getExtendedAccessToken($access_token);
/**
* To save Access tocken and other necessary option
*/
$usr_bus_model = new Application_Model_UserBusinessAccounts;
$usr_bus_model->loadAccount($user_id,'Facebook',(int)$fb_account_id,false);
$usr_bus_model->details=$extended_access_token;
if(!empty($extended_access_token))
{
$usr_bus_model->status= 'active';
}
$usr_bus_model->save();
return $extended_access_token;
}
public function getExtendedAccessToken($access_token)
{
$token_url="https://graph.facebook.com/oauth/access_token";
$params=array('client_id'=>self :: appId,'client_secret'=>self :: appSecretId,'grant_type'=>'fb_exchange_token','fb_exchange_token'=>$access_token);
$response = $this->curl($token_url,$params);
$response = explode ('=',$response);
$response = explode ('&',$response[1]);
$response = $response[0];
return $response;
}
我做了以上两个功能。因此,每当您需要使用访问令牌时,只需调用扩展访问令牌并将其保存到您的数据库中。所以你的访问令牌永远不会过期。