如何在页面上显示facebook的响应/获取facebook用户的数据

How to get display the facebook response on the page/obtain the data of facebook user?

本文关键字:facebook 响应 获取 数据 用户 显示      更新时间:2023-09-26

我是一个创建Facebook选项卡的初学者。我尝试使用Facebook Javascript SDK (https://developers.facebook.com/docs/reference/javascript/)来创建它。我知道当用户授权了他/她的信息后,有一个响应存储了这个信息。但是,我不知道如何获得这个值才能在网页上显示。我只知道在这种情况下应该使用ajax,但我对ajax几乎一无所知。所以我想知道是否有样例代码可以解决这个问题。谢谢你。

在index.php中有一部分代码。我正在使用Slim框架和红豆。

$app->map('/intro', function () use($app) {
    $app_id = "XXXXXXXXX";
    $app_secret = "XXXXXXXXXXX";
    $introduction = R::findOne('introduction', 'id = 1');
    $name = $introduction->name;
    $description = $introduction->description;
    $req = $app->request();
    $var = $req->post('args');
    print_r($var);
    $data = array(
        'title' => $name,
        'heading' => $description,
        'app_id' => $app_id,
        'app_secret' => $app_secret,
        'uid' => $var.id
    );
    $app->render('/question/tpl_intro.php', $data);
})->via('GET', 'POST');

facebook API使用基于事件的系统。这对你来说意味着,你需要注册回调函数,当某些事件发生在他们的系统上时,facebook可以调用。

在链接fb jssdk后需要做的第一件事是添加fb .init()调用。这个调用完成后,你可以钩入你想要为用户加载详细信息的任何javascript。
<script type='text/javacript'>
    function AttachHandlers() {
        // getLoginStatus checks if the user is logged in and authorized your app, just
        // logged in and hasn't authorized your app, or isn't logged in.
        FB.getLoginStatus(function(response) {
            if (response.status == 'connected') {
                // if they're logged in and you're authorized, you can actually
                // query facebook for details on the user. 
                FB.api('/me', function(data) {
                    //data will have user details in it.
                    //console.log(data);
                    local args = {name: ''};
                    if (data.email)
                        args.email = data.email;
                    if (data.first_name)
                        args.name = data.first_name;
                    if (data.last_name)
                        args.name += ' ' + data.last_name;
                    // and on and on for as many properties as you need
                    $.post('http://www.yoursite.com/posthandler.php', 
                           args, 
                           function (d, status, xhr) { console.log(d); });
                });
            }
        });
    }
    window.fbAsyncInit = function () {
        FB.init({appId: 'yourID', status: true, cookie: true, oauth: true, xfbml: true});
        // now the facebook api is loaded properly, you can now start using it.
        AttachHandlers();
    }
</script>

基本上,一旦库被加载-你只需传递给它一些你自己的函数,它可以调用这些函数来更新你在facebook上的用户发生的事情(登录/退出,授权/不授权你的应用程序,等等)