2012-03-20 75 views
0

我想如果用戶登錄Facebook,如果是的話我需要將他們的個人資料圖片url,名稱存儲在我的數據庫中,以便用戶可以在我的web應用程序中發表評論。oauth與facebook

我無法做到這一點,我創建了一個Facebook的網站應用程序,並使用下面的代碼auth目的。但其顯示「出現錯誤,請稍後再試。」

任何人請幫助我?或幫助我工作的代碼?

<?php 
require_once('../src/facebook.php'); 

$fb_app_id = "key"; 
$fb_secret = "secret"; 
$fb_app_url = "url"; 
$facebook = new Facebook(array(
    'appId' => $fb_app_id, 
    'secret' => $fb_secret, 
    'cookie' => true, 
)); 

$user = $facebook->getUser(); 

if ($user) { 
    // The user is logged in 
    try { 
    $user_profile = $facebook->api('/me'); 
    // Here : API call succeeded, you have a valid access token 
    } catch (FacebookApiException $e) { 
    // Here : API call failed, you don't have a valid access token 
    // you have to send him to $facebook->getLoginUrl() 
    $user = null; 
    } 
} // else : the user is not logged in 


?> 



<?php if ($user): ?> 
    <a href="<?php echo $facebook->getLogoutUrl() ?>">Logout of Facebook</a> 
<?php else: ?> 
    <a href="<?php echo $facebook->getLoginUrl() ?>">Login with Facebook</a> 
<?php endif ?> 
+1

嘗試,如果($ user_profile),而不是 max4ever 2012-03-20 15:09:31

+0

您好我試過了...但仍顯示出錯誤.. 。這個腳本是否以app_key和密鑰工作到最後? Facebook方面的任何特殊變化? – ramesh 2012-03-20 15:17:23

回答

1

你可以試試這個

require_once('../src/facebook.php'); 
$facebook = new Facebook(array(
    'appId' => 'xxxxxxxxxxxx', 
    'secret' => 'xxxxxxxxxxxx', 
    'cookie' => true, 
)); 

$fb_user_id = $facebook->getUser(); 

try 
{ 
$fb_user_profile = $facebook->api('/me'); 
$me = $facebook->api('/me'); 
} 
catch (FacebookApiException $e) 
{ 

$fb_user_id = NULL; 
} 
$fbid = $me['id']; 
$firstname = $me["first_name"]; 
$lastname = $me["last_name"]; 
$gender = $me["gender"]; 
$email = $me["email"]; 

if ($facebook->getSession()) { 
    echo '<div id="fbc-img"><img src="http://graph.facebook.com/'.$me['id'].'/picture?type=normal" width="56" height="56"></div> 
    <div id="fbc-info">Welkom, '.$firstname.' ! <br><a href="' . $facebook->getLogoutUrl() . '">Logout </a></div> '; 

} else { 
    echo '<a href="https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_WEBSITE&scope=email" target="_blank">Login met facebook</a> '; 
} 
+0

感謝您的幫助,當我嘗試我收到此消息 致命錯誤:調用未定義的方法Facebook :: getSession()在/home/aasrayac/public_html/domain.com/fb/examples/index.php在線28 – ramesh 2012-03-20 16:08:08

+0

你有沒有更新你的Facebook PHP SDK?這裏是一個鏈接https://github.com/facebook/php-sdk你需要把SRC文件夾放在你的根目錄下,並且不要忘記指出require_once到facebook.php文件 – 2012-03-21 08:08:54

+0

看來你沒有或沒有指出正確的facebook.php文件 – 2012-03-21 11:20:34