0

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

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

$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');
4

1 回答 1

1

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 上发生事情时(登录/注销、授权/取消授权你的应用程序等),它可以调用这些函数来更新你。

于 2013-05-22T19:57:46.300 回答