$facebook->api('/me') is returning NULL.

Discussion in 'PHP' started by FutureKing, Mar 9, 2013.

  1. #1
    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:
     
    FutureKing, Mar 9, 2013 IP
  2. D3Tek

    D3Tek Active Member

    Messages:
    164
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    50
    #2
    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.
     
    D3Tek, Mar 11, 2013 IP
  3. FutureKing

    FutureKing Well-Known Member

    Messages:
    194
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    125
    Digital Goods:
    4
    #3
    I just took off $facebook->api('/me') from try catch block and everything started working.
     
    FutureKing, Mar 12, 2013 IP
  4. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    160
    #4
    @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.
     
    edduvs, Mar 13, 2013 IP
  5. Vick.Kumar

    Vick.Kumar Active Member

    Messages:
    138
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    90
    #5
    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):
     
    Vick.Kumar, Mar 13, 2013 IP
  6. D3Tek

    D3Tek Active Member

    Messages:
    164
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    50
    #6

    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.
     
    D3Tek, Mar 13, 2013 IP