2012-05-13 2 views
5

저는 PHP와 MySQL에 완전히 익숙하지만 웹 사이트에 Facebook PHP SDK를 구현하고 있습니다. 모든게 지금까지 잘 작동하지만 내 데이터베이스 (MySQL은)에 사용자 데이터를 추가하는 고투하고 있어요. 내가 가진 모든 것은 사용자 이름과 oauth_uid 대신에 숫자입니다 (두 필드 모두에 대해 5를 얻습니다). 이 코드입니다 :Facebook PHP SDK - 사용자 데이터를 MYSql 데이터베이스에 저장하십시오.

<?php 
    define('db_user','myUserName'); 
    define('db_password','myPassword'); 
    define('db_host','myHost'); 
    define('db_name','myDbName'); 

    // Connect to MySQL 
    $dbc = @mysql_connect (db_host, db_user, db_password) OR die ('Could not connect to MySQL: ' . mysql_error()); 
    // Select the correct database 
    mysql_select_db (db_name) OR die ('Could not select the database: ' . mysql_error()); 

    require 'lib/facebook.php'; 

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


    // 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. 
    $fbuid = $facebook->getUser(); 
    $user_profile = $facebook->api('/me'); 



     } catch (FacebookApiException $e) { 
     error_log($e); 
     $user = null; 
     } 
     }else{ 
     header('Location: index.php'); 

     } 
      $query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND  oauth_uid = ". $user['id']); 
      $result = mysql_fetch_array($query); 

     if(empty($result)){ 
      $query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username)   VALUES ('facebook', {$user['id']}, '{$user['name']}')"); 
      $query = msyql_query("SELECT * FROM users WHERE id = " . mysql_insert_id()); 
      $result = mysql_fetch_array($query); 
      } 



     // Login or logout url will be needed depending on current user state. 
     if ($user) { 
     $paramsout = array('next'=>'http://www.mywebsite.com/test/logout.php'); 
     $logoutUrl = $facebook->getLogoutUrl($paramsout); 
     } 


     ?> 

위의 페이지 user_page.php라고하며 사용자가 로그인 후 리디렉션되는 곳이다. 나는 "데이터베이스"코드를 index.php (사용자가 로그인하는 곳)로 이동하려고 시도했지만 지금까지는 모든 노력이 실패했습니다. 문제가 해결 *

require 'lib/facebook.php'; 

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


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


    if ($user) { 
    try { 
     // Proceed knowing you have a logged in user who's authenticated. 
     $fbuid = $facebook->getUser(); 
     $user_profile = $facebook->api('/me'); 

     header('Location: user_page.php'); 

     } 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(Array('scope'=> 'user_interests,user_activities,user_education_history,user_likes,user_about_me, user_birthday, user_groups, user_hometown, user_work_history, email', 
      'redirect_uri' => 'http://www.mywebpage.com/test/user_page.php') 
     ); 
      } 

      ?> 

UPDATE ($ user_profile로 대신의 $ 사용자) : 코드에서

$query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND oauth_uid = ". $user_profile['id']); 
    $result = mysql_fetch_array($query); 

    if(empty($result)){ 
    $query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username) VALUES ('facebook', {$user_profile['id']}, '{$user_profile['name']}')"); 
    $query = msyql_query("SELECT * FROM users WHERE id = " . mysql_insert_id()); 
    $result = mysql_fetch_array($query); 
    } 

답변

8

페이스 북 사용자의 데이터가 저장이 index.php에 코드입니다 $user_profile 이하이고 $user이 아님.

+0

정말 고맙습니다. – mat

+0

당신은 환영합니다 :) –

관련 문제