2014-12-19 121 views
0

嗨我試圖從登錄後的臉譜用戶從facebook用戶數據。但我的會話變量始終爲空。我目前正在使用laravel 4框架。您所看到的代碼被稱爲來自Facebook登錄的回調函數。會話總是返回空facebook facebook sdk

$code = Input::get('code'); 
    if (strlen($code) == 0) return Redirect::to('/')->with('message', 'There was an error communicating with Facebook'); 

    $facebook = new Facebook(Config::get('facebook')); 
    $uid = $facebook->getUser(); 

    if ($uid == 0) return Redirect::to('/')->with('message', 'There was an error'); 

    $me = $facebook->api('/me'); 

    $profile = Profile::whereUid($uid)->first(); 
    if (empty($profile)) { 

     $user = new User; 
     $user->name = $me['first_name'].' '.$me['last_name']; 
     $user->email = $me['email']; 
     $user->save(); 

     $profile = new Profile(); 
     $profile->uid = $uid; 
     return $uid; 
     $profile->username = $me['name']; 
     $profile = $user->profiles()->save($profile); 
    } 

    $profile->access_token = $facebook->getAccessToken(); 
    $profile->save(); 

    $user = $profile->user; 

    Auth::login($user); 
    FacebookSession::setDefaultApplication(
     Config::get('facebook.appId'), 
     Config::get('facebook.secret') 
    ); 

    $helper = new FacebookRedirectLoginHelper('http://projectweb.app:8000/'); 
    $session = $helper->getSessionFromRedirect(); 

    if(isset($session)) 
    { 
     $request = new FacebookRequest($session,'GET','/me'); 
     $response = $request->execute(); 
     $graph = $response->getGraphObject(GraphUser::classname()); 
     $name = $graph->getName(); 
     return $name; 
    } 
    else 
    { 
     return 'no sesssion'; 
    } 

回答

相關問題