2012-04-16 13 views
0

私はFacebook Loginボタンを使用して、私のウェブサイトのFacebookに統合します。 しかし、facebookを使ってログインする際に、ユーザー名など(Facebookのユーザーデータ)をFacebookのデータベースに保存する方法がわかりません。どのようにこれを行うことができますか?facebook login buttonプラグインを使用してウェブサイトとFacebookの統合を行い、データベースにユーザーcreanditialsを保存します

現在、Facebookのログインボタンをクリックすると、ウィンドウが開き、Facebookのユーザー名とパスワードを入力すると接続されます。

Facebookのユーザーデータを保存するためにログイン中にPHPの部分に入力する方法と、既に存在する場合はデータを保存せずにサイトにログインしますか?

<div id="fb-root"></div> 
<script>(function(d, s, id) { 
    var js, fjs = d.getElementsByTagName(s)[0]; 
    if (d.getElementById(id)) return; 
    js = d.createElement(s); js.id = id; 
    js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=987654321"; 
    fjs.parentNode.insertBefore(js, fjs); 
}(document, 'script', 'facebook-jssdk'));</script> 


    // facebook login button 

<div class="fb-login-button" data-show-faces="false" data-width="200" data-max-rows="1" ></div> 
+0

スタート[FB PHP SDK](https://github.com/facebook/facebook-php-sdk) – zerkms

+0

のようなので、あなたが保存したいですあなたのデータベースのユーザーはFacebookのログインとパスワードですか? – Asdfg

+0

@Asdfgいいえ、username/email_id、アクセストークンのようなその他の詳細のみです。 –

答えて

1

facebook php sdkをダウンロードしてください。

以下は、Facebookに接続するための例です。ログイン部分チェックで

<?php session_start(); 
require '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(); 
//$session = $facebook->getSession(); 
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) { 
    foreach($user_profile as $skey => $sval) 
    { 
     $_SESSION["$skey"]=$sval; 
    } 
    $_SESSION['fb_name']=$user_profile['name']; //get facebook username and set it in session 
    $_SESSION['fb_id']=$user_profile['id']; //get facebook id and set it in session 
    $logoutUrl = $facebook->getLogoutUrl(); 
    $_SESSION['fb_logout']= $logoutUrl; 

    ?> 
     <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.href="index.php"; 
      //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> 

発見とこの

if(isset($_SESSION['fb_id'])) { 
//check whether the entry for this fb_id already exists in db 
//if not exists insert id and name into db, else get username from db for this fb_id 
} 
関連する問題