2011-12-20 12 views
0

php-sdkを使ってFacebookに接続しようとしています。 Facebookのログイン画面が表示されます。ログインをクリックすると、ユーザー名とパスワードが入力され、同じページにリダイレクトされ、接続されていません。私はfacebook phpで新しいです。助けてくださいFacebook php connect

<?php 
    require_once("php-sdk/src/facebook.php"); 

// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
    'appId' => '---', 
    '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; 
    } 
} 

// 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>php-sdk</title> 
    <style> 
     body { 
     font-family: 'Lucida Grande', Verdana, Arial, sans-serif; 
     } 
     h1 a { 
     text-decoration: none; 
     color: #3b5998; 
     } 
     h1 a:hover { 
     text-decoration: underline; 
     } 
    </style> 
    </head> 
    <body> 
    <h1>php-sdk</h1> 

    <?php if ($user): ?> 
     <a href="<?php echo $logoutUrl; ?>">Logout</a> 
    <?php else: ?> 
     <div> 
     Login using OAuth 2.0 handled by the PHP SDK: 
     <a href="<?php echo $loginUrl; ?>">Login with Facebook</a> 
     </div> 
    <?php endif ?> 

    <h3>PHP Session</h3> 
    <pre><?php print_r($_SESSION); ?></pre> 

    <?php if ($user): ?> 
     <h3>You</h3> 
     <img src="https://graph.facebook.com/<?php echo $user; ?>/picture"> 

     <h3>Your User Object (/me)</h3> 
     <pre><?php print_r($user_profile); ?></pre> 
    <?php else: ?> 
     <strong><em>You are not Connected.</em></strong> 
    <?php endif ?> 
    </body> 
</html> 
+0

Facebookのアプリ設定でコールバックURLを確認する必要があります。 – Arfeen

答えて

0

あなたが作成したばかりのアプリに特定の情報を提供する必要があります。コールバックURLが必要です。 Facebookの最後にある情報を確認してください。

1

appIdsecretを置き、次のコードに従ってください。最新の情報も使用する必要があります。Facebook PHP SDK

$facebook = new Facebook(array(
    'appId' => '111111111',// your appId here 
    'secret' => '1a1a1a1a1a1a1',// your app secret here 
)); 


$user = $facebook->getUser(); 

if ($user) { 
    try { 
    $user_profile = $facebook->api('/me'); 

    } catch (FacebookApiException $e) { 
    error_log($e); 
    $user = null; 
    } 
} 

if ($user) { 
    $logoutUrl = $facebook->getLogoutUrl(); 



} else { 
    $loginUrl = $facebook->getLoginUrl(); 
    echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>"; 
    exit; 
}