Php Facebook连接,然后显示用户';第一+;姓氏和个人资料图片

Php Facebook连接,然后显示用户';第一+;姓氏和个人资料图片,php,html,facebook,Php,Html,Facebook,我希望新用户能够在我的网站上使用fb connect按钮“连接”,一旦连接,它将显示他们的名字+姓氏和个人资料图片 如果用户已连接,则“连接”按钮将变为注销 根据文档,我实现了以下代码 <html> <head> <title>test full name and photo</title> </head> <body> <div id="fb-root"><

我希望新用户能够在我的网站上使用fb connect按钮“连接”,一旦连接,它将显示他们的名字+姓氏和个人资料图片

如果用户已连接,则“连接”按钮将变为注销

根据文档,我实现了以下代码

<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>

测试全名和照片
FB.init({
appId:'xxxxxxxxx',cookie:true,
状态:true,xfbml:true
});
FB.api('/me',函数(用户){
如果(用户!=null){
var image=document.getElementById('image');
image.src=http://graph.facebook.com/“+user.id+”/picture”;
var name=document.getElementById('name');
name.innerHTML=user.name
}
});
唯一的问题是它没有显示FacebookConnect按钮,当我尝试插入它时,代码中断

任何帮助都将不胜感激

概述:网站检查用户是否已连接并拥有有效会话,如果未连接,则显示fb连接/登录按钮,如果是,则显示注销按钮,无论哪种方式,都应显示用户的姓名和个人资料照片

谢谢大家!

将完全满足您的要求:

<?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>

测试全名和照片
window.fbAsyninit=函数(){
FB.init({
appId:“”,//应用ID
channelURL:'//WWW.YOUR_DOMAIN.COM/channel.html',//频道文件
状态:true,//检查登录状态
cookie:true,//启用cookie以允许服务器访问会话
oauth:true,//启用oauth 2.0
xfbml:true//解析xfbml
});
//每当用户登录时,我们都会刷新页面
FB.Event.subscribe('auth.login',function()){
window.location.reload();
});
};
(功能(){
var e=document.createElement('script');
e、 src=document.location.protocol+'//connect.facebook.net/en_US/all.js';
e、 异步=真;
document.getElementById('fb-root').appendChild(e);
}());
使用JavaScript&;XFBML:
/图片“/>

你不客气!请考虑投票吧!