douzhiji2020 2011-02-05 05:39
浏览 100
已采纳

Facebook Connect然后显示用户的第一个+姓氏和个人资料图片

I'd like new users to be able to 'connect' with the use of the fb connect button on my site and once connected it will display their first+last name and profile picture.

If the user is already connected, the connect button turns into a logout.

Going by the docs, i've implemented the following code;

<html>
    <head>
      <title>test full name and photo</title>
    </head>
    <body>
      <div id="fb-root"></div>
      <script src="http://connect.facebook.net/en_US/all.js">
      </script>
      <script>
         FB.init({ 
            appId:'xxxxxxxxxx', cookie:true, 
            status:true, xfbml:true 
         });
         FB.api('/me', function(user) {
           if(user != null) {
              var image = document.getElementById('image');
              image.src = 'http://graph.facebook.com/' + user.id + '/picture';
              var name = document.getElementById('name');
              name.innerHTML = user.name
           }
         });
       </script>
           <div align="center">
           <img id="image"/>
           <div id="name"></div>
           </div>
    </body>
 </html>

The only problem is that it does not display the facebook connect button and when I try and insert it, the code breaks.

Any help would be much appreciated!

recap: website checks if the user has connected and has a valid session, if not, display fb connect/login button, if yes, display the logout button, either way it should show the user their name and profile photo.

Thank you!

  • 写回答

1条回答 默认 最新

  • drep94225 2011-02-05 15:02
    关注

    The Facebook PHP-SDK will do exactly what you want:

    <?php
    
    require '../src/facebook.php';
    
    // Create our Application instance (replace this with your appId and secret).
    $facebook = new Facebook(array(
        'appId'  => 'APP_ID',
        'secret' => 'APP_SECRET',
    ));
    
    $user = $facebook->getUser();
    // Session based API call.
    if ($user) {
        try {
            $me = $facebook->api('/me');
        } catch (FacebookApiException $e) {
            echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
            $user = null;
        }
    }
    
    // login or logout url will be needed depending on current user state.
    if ($user) {
      $logoutUrl = $facebook->getLogoutUrl();
    } else {
      $loginUrl = $facebook->getLoginUrl();
    }
    ?>
    <!DOCTYPE html>
    <html xmlns:fb="http://www.facebook.com/2008/fbml">
    <head>
        <title>test full name and photo</title>
    </head>
    <body>
        <div id="fb-root"></div>
        <script>
        window.fbAsyncInit = function() {
            FB.init({
                appId      : '<?php echo $facebook->getAppId(); ?>', // App ID
                channelURL : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
                status     : true, // check login status
                cookie     : true, // enable cookies to allow the server to access the session
                oauth      : true, // enable OAuth 2.0
                xfbml      : true  // parse XFBML
            });
    
            // whenever the user logs in, we refresh the page
            FB.Event.subscribe('auth.login', function() {
              window.location.reload();
            });
        };
    
        (function() {
            var e = document.createElement('script');
            e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
            e.async = true;
            document.getElementById('fb-root').appendChild(e);
        }());
        </script>
        <?php if ($user): ?>
        <a href="<?php echo $logoutUrl; ?>">
          <img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">
        </a>
        <?php else: ?>
        <div>
          Using JavaScript &amp; XFBML: <fb:login-button></fb:login-button>
        </div>
        <?php endif ?>
    
    
        <?php if ($user): ?>
        <div align="center">
            <img id="image" src="https://graph.facebook.com/<?php echo $uid; ?>/picture" />
            <div id="name"><?php echo $me['name']; ?></div>
        </div>
        <?php endif ?>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值