I am trying to use Facebook connect in Yii framework. The problem is I want to access user's email address and $facebook->api('/me') is returning NULL. How to fix that problem? Here is my code: <?php Yii::import("ext.fconnect.*"); $app_id = "xxx"; $app_secret = "xxxx"; Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false; $facebook = new Facebook(array( 'appId' => $app_id, 'secret' => $app_secret, )); $user = $facebook->getUser(); if ($user) { try { $user_profile = $facebook->api('/me'); } catch (FacebookApiException $e) { $user_profile = $e; $user = NULL; } } if ($user) { $d["logoutUrl"] = $facebook->getLogoutUrl(array( 'fblogout' => 'true', 'next' => $this->createAbsoluteUrl("main/signout") )); $d["user_info"] = $facebook->api('/' . $user); } else { $d["loginUrl"] = $facebook->getLoginUrl(array( 'scope' => 'email, read_stream, publish_stream, user_birthday, user_location, user_work_history, user_hometown, user_photos', 'redirect_uri' => $this->request->hostInfo . $this->request->url, )); } $d["user"] = $user; $d["userprofile"] = $user_profile; PHP:
Can you replace try { $user_profile = $facebook->api('/me'); } catch (FacebookApiException $e) { $user_profile = $e; $user = NULL; } PHP: with try { $user_profile = $facebook->api('/me'); } catch (FacebookApiException $e) { print_r($e); $user_profile = $e; $user = NULL; } PHP: To help track the issue.
@FutureKing, you don't need to use try catch since it's not a must ! You can just verify your arrays just by counting it or checking if it's empty after you do the request.
You should have a look at the yii-eoauth extention. You will need to do the handshakes first to authenticate the users and afterwards you will be able to get the required details. oAuth for Facebook oAuth for twitter oAuth for Google oAuth for Linked In class FBIdentity extends BaseUserIdentity { private $_id; public function getType() { return $_type; } // this is a hack and returns whether we need to register // amongst other things (unlike CUserIdentity) public function authenticate() { $this->_id = $this->password; // facebook-connect uses password for id // what follows hinges on a User model doing all the legwork... // my "User" has properties like: id, type, email, facebook $user = User::model()->findByAttributes(array("facebook" => $this->_id)); if($user === null) return self::ERROR_UNKNOWN_IDENTITY; // facebook user not found // alternatively you could CREATE the database user here... // $user = new User; // $user->facebook = $this->_id; // if(!$user->save()) return self::WHATEVER_YOU_COME_UP_WITH // ... etc ... // there are more things you can do here... // depending on what you need // e.g. I check whether the user is active // meaning: don't allow deactivated users... if($user->type == User::TYPE_DEACTIVATED) return self::ERROR_USER_DEACTIVATED; // user deactivated // you can also do a bunch of $this->setState()'s // e.g.: $this->setState("user_id", $user->id ); // see below... $this->setState("email" , $user->email); // which you can then use this in your other code like so: // Yii::app()->user->email // the user_id setting is interesting, as there is a // Yii::app()->user->id setting which is for FBIdentity the facebook-id // for your regular users it would be the user database-id // to unify these two different user session types I added // a new state "user_id", which always contains that database-id, // so: // for regularly logged-in Yii::app()->user both // Yii::app()->user->id and // Yii::app()->user->user_id contain the database user-id // while for Yii::app()->user logged in via facebook-connect // Yii::app()->user->id contains the facebook-id, while // Yii::app()->user->user_id contains the user database-id // hence... // whatever you add here, in order for this to work across // ALL sessions, including people NOT logging in via facebook-connect, // you'd have to add similar features in your // // class WebUser extends CWebUser // // in your components directory, which you activate via // e.g. a config/main.php setting like: // // 'components' => array( // 'user' => array('class' => 'WebUser'), // ... // remember... the Yii::app()->user object reflects a different // thing (namely FBIdentity here) when people log-in via // facebook-connect as opposed to the regular way... // but this integration/duplication work is left to you. return self::ERROR_NONE; } public function getId() { return $this->_id; } } Code (markup):
The use of Exceptions here is just fine. We expect the API to return a result, which it's not doing, so we throw an Exception. The Exception then jumps out of the method and chases the trace until it's caught and handled. I'm not saying you're wrong, and it can be handled differently, but Exceptions here are fine and doing the job perfectly. @FutureKing - glad you solved the issue.