facebook sdk php example not working

馋奶兔 提交于 2019-12-05 16:26:33

Facebook user can return 0 if there is no user logged in. The case that you do have a user means that you have a user logged in to facebook via your application. That also does not mean that you have permissions to view the user's details. Try this simple example I wrote some time ago but still works.

Now take care this is a redirect controller. As you can see it only checks and asks for user's. Use it as an 'intermediate' controller and base your templating HTML logic with the JSFBSDK or every time you need a user to take an action redirect him here (eg login etc)

// 0. We don't need to clear session
require_once realpath(dirname(__FILE__)) . 'facebook.php';

$current_url = ''; // replace

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

$fbCurrentUserID = $facebook->getUser();

// 1. If we cant get the user log him in and request permissions This requests combined permissions (basic + post)
if (!$fbCurrentUserID){
    $loginUrl = $facebook->getLoginUrl(array(
        'scope' => 'email, publish_actions', // Put the permissions you like            
        'redirect_uri' => $current_url, // Replace here
    ));
    die(header('Location: ' . $loginUrl));
}

// 2. So we do have a current FB user. Lets try to read data (Maybe he declined perms)
try {

    $user_profile = $facebook->api('/me');

} catch (FacebookApiException $e) {
    // 2.b Log any error and retry please

    die(header('Location: /'));
}

//Continue your code execution here after all up above went well
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!