使用新的PHP SDK(3.X.X)寻求许可 [英] Asking for permission using new PHP SDK (3.X.X)

查看:66
本文介绍了使用新的PHP SDK(3.X.X)寻求许可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I ask for permissions using new PHP SDK? I don't want to use the graph api and parse the url all the time. When the application is opened it should automatically ask for permissions if the user hasn't granted one already.

推荐答案

这就是我使用最新的PHP SDK(3.0.1)的方式

Here's how i'm doing it with the latest 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'];

这篇关于使用新的PHP SDK(3.X.X)寻求许可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆