2011-08-14 66 views
0

我該從哪裏出發?這幾乎只是從sdk中提供的示例複製粘貼。我不明白人們如何使用這個API來構建任何東西?我如何打開登錄等提示屏幕? Facebook在那裏說了些什麼?Facebook API幫助

<?php 

require 'fb_sdk/src/facebook.php'; 

// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
    'appId' => 'APIID', 
    'secret' => 'SECRET', 
)); 

// Get User ID 
$user = $facebook->getUser(); 

// We may or may not have this data based on whether the user is logged in. 
// 
// If we have a $user id here, it means we know the user is logged into 
// Facebook, but we don't know if the access token is valid. An access 
// token is invalid if the user logged out of Facebook. 

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

// Permissions requested from the user. 
$par = array(); 
$par['scope'] = 'user_about_me, read_friendlists'; 

// Login or logout url will be needed depending on current user state. 
if ($user) { 
    $logoutUrl = $facebook->getLogoutUrl(); 
} else { 
    $loginUrl = $facebook->getLoginUrl($par); 
} 

?> 

回答

1

你可以找到有關使用圖形用戶認證here on Facebook Developers的信息。

使用$loginUrl = $facebook->getLoginUrl($par);變量$loginUrl將包含驗證對話框的URL。大多數開發人員無論是目前這給用戶一個鏈接或執行的JavaScript重定向 - 如:

die('<script>top.location.href = "' . $loginUrl . '"</script>'); 

另一種方法是使用JavaScript SDK與XFBML進行身份驗證(如果您有餅乾使得能夠利用軟件開發工具包,他們將共享會話數據) - 從here例如:

<?php 

require 'php-sdk/src/facebook.php'; 

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

// 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_profile) { ?> 
     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> 

您還可以使用JavaScript純粹登錄使用FB.Login

FB.login(function(response) { 
    if (response.authResponse) { 
    console.log('Welcome! Fetching your information.... '); 
    FB.api('/me', function(response) { 
     console.log('Good to see you, ' + response.name + '.'); 
     FB.logout(function(response) { 
     console.log('Logged out.'); 
     }); 
    }); 
    } else { 
    console.log('User cancelled login or did not fully authorize.'); 
    } 
}, {scope: 'user_about_me, read_friendlists'}); 
+0

g^ood answer,至於@Jollygood:不要爲此考慮API。它在那裏*幫助*你的應用程序。一旦你清楚瞭解你的應用程序將提供什麼,那麼與Facebook API的集成點就會明確。以這個[示例](http://developers.facebook.com/docs/samples/canvas/)應用程序爲例,您會意識到您的應用程序創意,並得出結論,您需要用戶全名,電子郵件,照片和來自Facebook的朋友並相應地使用API​​。 – ifaour

0

它在評論中提到:

// Login or logout url will be needed depending on current user state. 
if ($user) { 
    $logoutUrl = $facebook->getLogoutUrl(); 
} else { 
    $loginUrl = $facebook->getLoginUrl($par); 
} 

SDK將採取照顧,你不用擔心。 用戶的詳細信息在$ user_profile 做一個echo"<pre>"; print_r($user_profile); echo"</pre>";,你會得到它。