使用新的PHP SDK(3.XX)请求权限

Kin*_*ien 12 php permissions facebook

如何使用新的PHP SDK请求权限?我不想使用图形api并一直解析网址.当应用程序打开时,如果用户尚未授予权限,则应自动请求权限.

Hen*_*nar 20

以下是我使用最新的PHP SDK(3.0.1)进行操作的方法

// init new facebook class instance with app info (taken from the DB)
$facebook = new Facebook(array(
    'appId' => 'YOUR APP ID',
    'secret' => 'YOUR APP SECRET'
));
// get user UID
$fb_user_id = $facebook->getUser();

    // get the url where to redirect the user
$location = "". $facebook->getLoginUrl(array('scope' => 'publish_stream, email'));

// check if we have valid user
if ($fb_user_id) {
    try {
        // Proceed knowing you have a logged in user who's authenticated.
        $fb_user_profile = $facebook->api('/me');   

    } catch (FacebookApiException $e) {
        $fb_user_id = NULL;
        // seems we don't have enough permissions
        // we use javascript to redirect user instead of header() due to Facebook bug
        print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>';

        // kill the code so nothing else will happen before user gives us permissions
        die();
    }

} else {
    // seems our user hasn't logged in, redirect him to a FB login page

    print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>';

    // kill the code so nothing else will happen before user gives us permissions
    die();
}

// at this point we have an logged in user who has given permissions to our APP
// basic user info can be fetched easily
print "Welcome to my app". $fb_user_profile['name'];
Run Code Online (Sandbox Code Playgroud)