且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自定义Facebook连接按钮

更新时间:2023-12-04 22:46:46

只需使用JavaScript SDK的FB.login方法,并在你自己的按钮上点击它。



http://developers.facebook.com/docs/reference/javascript/FB.login/


How would I replace the generated facebook login button with my own custom one?

I am guessing it can either be done by creating a function of the code that fires up the dialog or driving the button off the screen with some css tricks. I'd prefer the first solution.

How could I make my own button do what theirs does and get rid of the the default button?

Here's the example code from the facebook developers site:

require 'fb/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'X',
  'secret' => 'X',
));

// See if there is a user from a cookie
$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
}

?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <body>
    <?php if ($user) { ?>
      Your user profile is
      <pre>
        <?php print htmlspecialchars(print_r($user_profile, true)) ?>
      </pre>
    <?php } else { ?>
      <fb:login-button></fb:login-button>
    <?php } ?>
    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId: '<?php echo $facebook->getAppID() ?>',
          cookie: true,
          xfbml: true,
          oauth: true
        });
        FB.Event.subscribe('auth.login', function(response) {
          window.location.reload();
        });
        FB.Event.subscribe('auth.logout', function(response) {
          window.location.reload();
        });
      };

      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());

    </script>
  </body>
</html>

Just use FB.login method of the JavaScript SDK, and call it onclick on your own button.

http://developers.facebook.com/docs/reference/javascript/FB.login/