开发者

How do I determine if there is an active facebook access token?

I am using the PHP Facebook 3.0 SDK for the graph API and want a simple way to find out if there is currently an active token so that I can handl开发者_如何转开发e errors gracefully.

getUserAccessToken() is a protected method and so I can't seem to access it with the facebook object. There has to be a simple way to do

if(active token){ do stuff} else{ don't}

Help anyone?


The Facebook PHP SDK Example has all the code you need for this:

<?php
require '../src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => '',
  'secret' => '',
));

$user = $facebook->getUser();

if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}
?>


Maybe you can use this type of code:

$data=file_get_contents('https://graph.facebook.com/me?access_token=xxx');
foreach ($http_response_header as $rh) if (substr($rh, 0, 4)=='HTTP') list(,$status,)=explode(' ', $rh, 3);
if ($status==200 && $data)
    //Token is good, parse data
else
    //Token is no good


Try this:

try {
    $User = $Facebook->api('/me');
} catch(Exception $e) {
    echo 'No active token.';
    echo $e->getMessage();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜