2013-07-18 43 views
0

我有一個用於登錄Laravel的Facebook身份驗證系統,當具有所有屬性的用戶登錄時工作正常,但是當用戶沒有填寫屬性,它拋出一個錯誤(例如):如何檢查一個屬性是否存在(FB認證)與Laravel Oauth2

未定義的屬性:stdClass的:: $生物

下面是相關代碼:

Facebook.php:

public function get_user_info(Token_Access $token) 
    { 
     $url = 'https://graph.facebook.com/me?'.http_build_query(array(
      'access_token' => $token->access_token, 
     )); 

     $user = json_decode(file_get_contents($url)); 

     // Create a response from the request 
     return array(
      'uid' => $user->id, 
      'nickname' => $user->username, 
      'name' => $user->name, 
      'first_name' => $user->first_name, 
      'last_name' => $user->last_name, 
      'gender' => $user->gender, 
      'email' => $user->email, 
      'birthday' => $user->birthday, 
      'location' => $user->location->name, 
      'description' => $user->bio, //the line giving trouble for a user 
      'friends' => 'https://graph.facebook.com/me/friends?fields=id&access_token='.$token->access_token, 
      'image' => 'https://graph.facebook.com/me/picture?width=200&height=200&access_token='.$token->access_token, 
      'urls' => array(
      'Facebook' => $user->link, 
      ), 
     ); 
    } 

Oauth2Controller.php:

if(is_null($check)) { 

        $fan = new Fan; 

        $fan->fbid = $user['uid']; 
        $fan->email = $user['email']; 
        $fan->first_name = $user['first_name']; 
        $fan->last_name = $user['last_name']; 
        $fan->gender = $user['gender']; 
        $fan->birthday = $user['birthday']; 
        $fan->age = $age; 
        $fan->city = $city; 
        $fan->state = $state_abbrev; 
        $fan->image = $user['image']; 
        $fan->friend_ids_url = $user['friends']; 
        $fan->friend_ids_unpacked = $friend_ids; 

        $fan->save(); 

        $new = Fan::where('fbid', '=', $user['uid'])->first(); 

        Auth::login($new); 
        return Redirect::to('fans/home'); 

       } 

如何檢查一個用戶擁有包含他們的個人資料,這些領域的,所以我沒有這個錯誤拋出?謝謝。

回答

2

你可以這樣做嗎?

'description' => (isset($user->bio) ? $user->bio : NULL)