2013-05-13 114 views
0


我正在使用FB id功能登錄我的應用程序,並開始從GITHUB(facebook-php-sdk-master)下載用於FB連接的SDK。
Facebook登錄使用FB PHP sdk不工作

在應用程序中它有兩個例子1)對於Javascript api和2)對於PHP api。 我想使用PHP SDK忽略用戶網站中的javascript禁用功能。
現在我進入我的應用程序

$facebook = new Facebook(array(
     'appId' => 'deliberately blank', 
     'secret' => '', 
    )); 

我登錄到Facebook的檢查,如果它的工作,它始終在返回即使用戶日誌。
我只是想打印的用戶變量

$user = $facebook->getUser(); 
    print_r($user); 

我檢查了應用程序設置,並且我已經爲域,URL和Canvas URL正確設置了它們。我也爲它禁用了沙盒模式。
這是插件的問題,因爲會話沒有被捕獲或我在這裏做錯了什麼?
任何工作或面臨類似的問題,請分享你的見解,我堅持,我試圖打印使用$_SESSION它返回具有

Array ([fb_117096311791424_state] => 5faba08750cd6456hbf27580df3b371) 

數組中的會話數據,但我不明白的用戶info.Is有沒有其他的方式來實現它?任何替代API等?

更新代碼

define('HTTP_SERVER', "http://".$_SERVER["HTTP_HOST"]); // eg, http://localhost - should not be empty for productive servers 
define('HTTPS_SERVER', ''); // eg, https://localhost - should not be empty for productive servers 
define('BASE_URL', HTTP_SERVER.'/'); // eg, https://localhost - should not be empty for productive servers 

if(HTTP_SERVER == 'http://localhost'){ 
    define('DOCUMENT_SERVER', $_SERVER['DOCUMENT_ROOT'].'/XYZ/'); 
}else{ 
    define('DOCUMENT_SERVER', $_SERVER['DOCUMENT_ROOT'].'/'); 
} 
require(DOCUMENT_SERVER.'includes/applicationTop.php'); 
$dataObj = new Database(); 
$generalObj = new General(); 

file_get_contents('http://www.ABC.com/login/facebookConnect/fbmain.php'); 
//require_once('http://www.ABC.com/login/facebookConnect/fbmain.php'); 

/** 
* check if user login first time or second time with facebook, records stored in table and session. 
**/ 

if (!empty($user)) 
{ 
$firstName = $userInfo['first_name']; 
$lastName = $userInfo['last_name']; 
$userName = $userInfo['username']; 
$gender = $userInfo['gender']; 
$email = $userInfo['email']; 
$birthday = $userInfo['birthday']; 

$userImage = $fqlResult[0]['pic_square']; 
$addressName = $fqlResult[0]['hometown_location']['name']; 
$addressCity = $fqlResult[0]['hometown_location']['city']; 
$addressState = $fqlResult[0]['hometown_location']['state']; 
$addressCountry = $fqlResult[0]['hometown_location']['country']; 
$addressZip = $fqlResult[0]['hometown_location']['zip']; 

$ip = $_SERVER['REMOTE_ADDR']; 
$date = date('Y-m-d'); 

$accountId = strtolower($generalObj->fnCreatePassword(8)); 


/** 
* check user first time login or not with facebook login. 
**/ 

$queryUser = "SELECT tbl_login.userId,tbl_login.accountId,tbl_login.emailId FROM tbl_login WHERE tbl_login.emailId='$email'"; 
$resultUser = $dataObj->tep_db_query($queryUser); 
$userInfo = $dataObj->getRecord($resultUser); 
$checkUser = $dataObj->getNumRows($queryUser); 

if($checkUser == '0' || $checkUser == '') 
{ 
    /** 
    * if user first time login with facebook then records are inserted into table. 
    **/ 
    $qyery = "INSERT INTO ".TABLE_LOGIN." (accountId, emailId,activeFlag,facebook_Userid) VALUES('$accountId','$email','1','$username')"; 
    $data = $dataObj->tep_db_query($qyery); 
    $userID = $dataObj->tep_db_insert_id(); 
    if(!empty($userID)) 
    { 
     $sql = "INSERT INTO ".TABLE_USER_INFO." (userId,fName,lName,address,city,country,state,postalCode,emailId,user_dob,profile_Image,user_ip,user_date,activeFlag) VALUES('$userID','$firstName','$lastName','$addressName','$addressCity','$addressCountry','$addressState','$addressZip','$email','$birthday','$userImage','$ip','$date','1')"; 
     $data = $dataObj->tep_db_query($sql); 
    } 
    //$_SESSION["gobiggiuserFaceBook"] = true; 
    $_SESSION["UserId"] = $userID; 
    $_SESSION["username"] = $accountId; 
    $_SESSION["emailId"] = $email; 
    $_SESSION["ProfileImage"] = $userImage; 

} 
else 
{ 
    /** 
    * updated user's info incase user has been updated own profile's records on facebook. 
    **/ 
    //QUery for update 

    /** 
    * If user have already login with facebook then records are taking into session 
    **/ 

    $_SESSION["UserId"] = $userInfo['userId']; 
    $_SESSION["username"] = $userInfo['accountId']; 
    $_SESSION["emailId"] = $userInfo['emailId']; 
    $_SESSION["ProfileImage"] = $userImage['profile_Image'];  

} 

$sessionUserid = (!empty($_SESSION["UserId"])) ? $_SESSION["UserId"] : '' ; 

if(!empty($sessionUserid)){ 
    echo "herererere in dashboard part"; 
    //exit; 
    header("Location:".USER_DASHBOARD."userDashboard.php"); 
    exit(); 
    //$generalObj->tep_redirect(FILE_PATH.'userDashboard.php'); 
}else{ 
    echo "herererere in login part"; 
    exit; 
    header("Location:".LOGIN."login.php"); 
    exit(); 
    //$generalObj->tep_redirect(FILE_PATH.'login.php'); 
} 
} 
else 
{ 
    //echo LOGIN.USER_DASHBOARD; 
    //exit(); 
    /*I always end up here*/ 
    header("Location:".LOGIN."login.php"); 
    exit(); 
    //$generalObj->tep_redirect(FILE_PATH.'login.php'); 
} 

這是fbmain.php

$fbconfig['appid' ]  = "deliberately blank"; 
$fbconfig['secret']  = " "; 
$fbconfig['baseurl'] = "http://www.ABC.com/login/facebookConnect/facebookHome.php"; 


if (isset($_GET['request_ids'])){ 
    //user comes from invitation 
    //track them if you need 
} 

$user   = null; //facebook user uid 
try{ 

    require_once "facebook.php"; 
} 
catch(Exception $o){ 
    error_log($o); 
} 
// Create our Application instance. 
$facebook = new Facebook(array(
    'appId' => $fbconfig['appid'], 
    'secret' => $fbconfig['secret'], 
    'cookie' => true, 
)); 

//Facebook Authentication part 
$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. 
//print_r($user); 
//exit; 
$loginUrl = $facebook->getLoginUrl(
     array(
      'scope'   => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown', 
      'redirect_uri' => $fbconfig['baseurl'] 
     ) 
); 

$logoutUrl = $facebook->getLogoutUrl(); 

if ($user) { 
    try { 
    // Proceed knowing you have a logged in user who's authenticated. 
    $user_profile = $facebook->api('/me'); 
    } catch (FacebookApiException $e) { 
    //you should use error_log($e); instead of printing the info on browser 
    d($e); // d is a debug function defined at the end of this file 
    $user = null; 
    } 
} 


//if user is logged in and session is valid. 
if ($user){ 
    //get user basic description 
    $userInfo   = $facebook->api("/$user"); 
    // print_r($userInfo); 
    //Retriving movies those are user like using graph api 
    try{ 
     $movies = $facebook->api("/$user/movies"); 
    } 
    catch(Exception $o){ 
     d($o); 
    } 

    //update user's status using graph api 
    //http://developers.facebook.com/docs/reference/dialogs/feed/ 
    if (isset($_GET['publish'])){ 
     try { 
      $publishStream = $facebook->api("/$user/feed", 'post', array(
       'message' => "", 
       'link' => '', 
       'picture' => '', 
       'name' => '', 
       'description'=> '' 
       ) 
      ); 
      //as $_GET['publish'] is set so remove it by redirecting user to the base url 
     } catch (FacebookApiException $e) { 
      d($e); 
     } 
     //echo "login successs!!!";exit; 
     $redirectUrl  = $fbconfig['baseurl'] . '/login.php?success=1'; 
     header("Location: $redirectUrl"); 
    } 

    //update user's status using graph api 
    //http://developers.facebook.com/docs/reference/dialogs/feed/ 
    if (isset($_POST['tt'])){ 
     try { 
      $statusUpdate = $facebook->api("/$user/feed", 'post', array('message'=> $_POST['tt'])); 
     } catch (FacebookApiException $e) { 
      d($e); 
     } 
    } 

    //fql query example using legacy method call and passing parameter 
    try{ 
     $fql = "select name, hometown_location, sex, pic_square from user where uid=" . $user; 
     $param = array(
      'method' => 'fql.query', 
      'query'  => $fql, 
      'callback' => '' 
     ); 
     $fqlResult = $facebook->api($param); 
    } 
    catch(Exception $o){ 
     d($o); 
    } 
} 

function d($d){ 
    //echo '<pre>'; 
    //print_r($d); 
    //echo '</pre>'; 
} 

的其他文件是從SDK本身。

問候

+0

所以基本上你可以登錄到Facebook並回到你的網站,但不能從fb獲得任何細節?!?!?! – 2013-05-13 12:56:18

+0

不,實際上如果我輸入了我的登錄信息,我沒有重定向到我的儀表板(登錄後的頁面),而是回到登錄頁面,沒有我的登錄痕跡。如果我已經登錄,在另一個選項卡上,它只是刷新並使我回到同一頁面。 **以某種方式你是正確的!** – coderunner 2013-05-13 12:59:57

+0

你能否發表你的完整代碼... – 2013-05-13 13:04:52

回答

1

。將file_get_contents('file_path');更改爲require_once('file_path');

。在其他情況下,你在哪裏檢查一日一次用戶,你已經使用

$_SESSION["UserId"] = $userInfo['userId']; 
$_SESSION["username"] = $userInfo['accountId']; 
$_SESSION["emailId"] = $userInfo['emailId']; 
$_SESSION["ProfileImage"] = $userImage['profile_Image']; 


所有這些值是錯誤的,我的意思是用於$userInfo指數是錯誤的!相反,使用方法如下:

$_SESSION["UserId"] = $userInfo['id']; //or $user 
$_SESSION["username"] = $userInfo['username']; 
$_SESSION["emailId"] = $userInfo['email']; 
$_SESSION["ProfileImage"] = $userImage; //not $userImage['profile_Image'] 


就這兩個變化,而你通過。

+0

它實際上就像這樣'$ sessionUserid =(!empty($ _ SESSION [「UserId」]))? $ _SESSION [「UserId」]:'';'更新了問題。我已將它設置爲會話'$ _SESSION [「UserId」] = $ userID;' – coderunner 2013-05-13 13:49:20

+0

好吧,讓我們調試並從基本開始。下載完文件後,請嘗試使用** example.php **文件中的代碼? – 2013-05-13 14:03:00

+0

是** example.php **正在工作! – coderunner 2013-05-13 14:08:38