EllisLab text mark
Advanced Search
     
Facebook SDK Problem retrieved user data
Posted: 11 November 2012 09:09 PM
Avatar
Joined: 2012-09-24
13 posts

Hi

I create in my website Facebook Login and it’s works fine, now I want to retrieved user data with API call method and insert in DB, but when I want retrieved user_birthday or any user Permissions I receive an error:

Undefined index: user_birthday

Could you help me.
Thanks

This is my model:

public function __construct()
 
{
  parent
::__construct();
  
  
$config = array(
   
'appId'  => 'myappid',
   
'secret' => 'myidsecret',
   
'fileUpload' => true
      );
  
  
$this->load->library('Facebook'$config);

  
  
$user $this->facebook->getUser();

  
$profile null;
  if(
$user)
  
{
   
try {

    $profile 
$this->facebook->api("/me"'get');

   
catch (FacebookApiException $e{
    error_log
($e);
       
$user null;
   
}  
  }
  
  $fb_data 
= array(
      
'me' => $profile,

      
'uid' => $user,
      
'loginUrl' => $this->facebook->getLoginUrl(
       array(
        
'scope' => 'email,publish_stream,user_about_me,user_checkins,publish_stream,friends_about_me,user_birthday',
                       
'redirect_uri' => base_url() . 'index.php/dashboard'
       
)
      ),
      
'logoutUrl' => $this->facebook->getLogoutUrl(
       array(
                       
'next' => base_url() 
       )
      ),
     );

  
$this->session->set_userdata('fb_data'$fb_data);
  
  
$this->db->get('account');
  
    
  
$db_data=array(
    
'name'=>$fb_data['me']['first_name']
    
'surname'=>$fb_data['me']['last_name'],
    
'nickname'=>$fb_data['me']['username'],
    
'slug'=>$fb_data['me']['first_name'.'.' .$fb_data['me']['last_name'],
    
'city'=>$fb_data['me']['username'],
    
'country'=> 'VUOTO',
    
'age'=>$fb_data['me']['user_birthday'],
    
'sex'=>$fb_data['me']['gender'],
    
'email'=>$fb_data['me']['email'],
    
'facebook'=>$fb_data['uid'],
    
'avatar'=> 'https://graph.facebook.com/'.$fb_data['uid'],
  );
  
  
$this->db->insert('account',$db_data);
 
 Signature 

Curreri Damiano
Software and Web Developer
 
  Phone: +44 (020) 365 942 71
  Fax:    +44 (020) 365 942 71
 
      http://www.damianocurreri.co.uk
      .(JavaScript must be enabled to view this email address)

 
Posted: 12 November 2012 01:27 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-07
552 posts

It says user_index is not in $fb_data[‘me’]. Did you check if it is?

echo '<pre>' print_r($fb_data)
 Signature 

View my Tutsplus course ‘Build a CMS with Codeigniter’
http://codeigniter.tv/ - Codeigniter video tutorials
Video tutorial series on MY_Model

 
Posted: 12 November 2012 10:44 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2012-09-24
13 posts

Thanks

I have check and you have right

user_birthday 
is not in
$fb_data['me'

Now how insert

user_birthday 
in
$fb_data['me'

?

I put in

scope 

declaration, but not work

 Signature 

Curreri Damiano
Software and Web Developer
 
  Phone: +44 (020) 365 942 71
  Fax:    +44 (020) 365 942 71
 
      http://www.damianocurreri.co.uk
      .(JavaScript must be enabled to view this email address)

 
Posted: 12 November 2012 12:41 PM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-07
552 posts

Not sure. It’s supposed to be called user_birthday in the scope, which then should be accessible as ‘birthday’. https://developers.facebook.com/docs/howtos/user-data-ios-sdk/

 Signature 

View my Tutsplus course ‘Build a CMS with Codeigniter’
http://codeigniter.tv/ - Codeigniter video tutorials
Video tutorial series on MY_Model

 
Posted: 12 November 2012 05:41 PM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2012-09-24
13 posts

thanks you’ve right

is requires to put the permissions in scope declaration and after use only ‘birthday’

thank

 Signature 

Curreri Damiano
Software and Web Developer
 
  Phone: +44 (020) 365 942 71
  Fax:    +44 (020) 365 942 71
 
      http://www.damianocurreri.co.uk
      .(JavaScript must be enabled to view this email address)

 
Posted: 12 November 2012 09:10 PM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2012-09-24
13 posts

Hi JoostV

Working with facebook skd I discovered something strange

I have start this post because I don’t for errors of birthday or for other user Permissions.

after your reply I continued too work but I had same problem.

after 3 hour I discovered that the error is not for my wrong code, but because the Facebook Account don’t have the relative field.

I’ll explain;

I proof the app with my wife account now she has not set her birthday, for this reason I had the error
same problem with bio (user_about_me) she has not set nothing into her About me, in fact after I insert two words in the About me(facebook) the app don’t show any error and I retrieved the bio fields.

Now, I want to know.. It’s is normal ? and if is normal how bypass this problem, off course not all people insert all info in Facebook Account.

thanks

 Signature 

Curreri Damiano
Software and Web Developer
 
  Phone: +44 (020) 365 942 71
  Fax:    +44 (020) 365 942 71
 
      http://www.damianocurreri.co.uk
      .(JavaScript must be enabled to view this email address)

 
Posted: 13 November 2012 03:32 AM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-07
552 posts

Haha, you never know what’s normal with Facebook wink

Anyway, it’s best to never trust any incoming data and the same goes for data coming from the Facebook API. Best do something like

echo !empty($fbdata['birthday']) ? htmlentities($fbdata['birthday']) : 'Unknown'

Or better yet, stick that in a helper function (see https://gist.github.com/4064663)
EDIT fixed a big in this code

/**
* Return the value for a key in an array or a property in an object.
* @param mixed $haystack
* @param string $needle
* @param mixed $default_value The value if key could not be found.
* @return mixed
*/
function get_key ($haystack$needle$default_value '')
{
if (is_array($haystack)) {
// We have an array. Find the key.
        
return isset($haystack[$needle]) ? $haystack[$needle] $default_value;
    
}
    
else {
     
// If it's not an array oit must be an object
     
return isset($haystack->$needle) ? $haystack->$needle $default_value;
    
}
}

// In your view, do stuff like
echo htmlentities(get_key($fbdata'birthday')); 
 Signature 

View my Tutsplus course ‘Build a CMS with Codeigniter’
http://codeigniter.tv/ - Codeigniter video tutorials
Video tutorial series on MY_Model

 
Posted: 14 November 2012 07:55 PM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2012-09-24
13 posts

Thanks

but I can use

function get_key ($haystack[quote] $needle[/quote] $default_value ''

my Hosting use PHP 5.2

because I don’t possible to use strstr with $needle parameter (is add after 5.3.0 version)

 Signature 

Curreri Damiano
Software and Web Developer
 
  Phone: +44 (020) 365 942 71
  Fax:    +44 (020) 365 942 71
 
      http://www.damianocurreri.co.uk
      .(JavaScript must be enabled to view this email address)

 
Posted: 14 November 2012 09:05 PM   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2012-09-24
13 posts
Hahayou never know what’s normal with Facebook ;-) 

You have right!!!!

but I think that I makes me mad grrr

Now explain because

5 hours ago I check my project and I discovered that Facebook login not works

Why, I think, upload the backup and fix the problem.

but nothing has changed.

the problem is that I don’t retrieved the Facebook UID, why if I don’t change any code?

Why one day works and another day no ?

this is my model:

$config = array(
  
'appId'  => 'XXXX',
  
'secret' => 'XXXX',
  
'fileUpload' => true
        );
  
  
$this->load->library('Facebook'$config);

  
$user $this->facebook->getUser();

  
  
$profile null;
  if(
$user)
  
{
   
try {
         $profile 
$this->facebook->api('/me');
    
   
catch (FacebookApiException $e{
    error_log
($e);
       
$user null;
   
}  
  }
  
  $fb_scope 
='email,
       user_birthday,
       user_about_me,
       user_website,
       user_interests,
       user_checkins,
       publish_stream,
       read_stream,
       read_friendlists
       '
;
  
  
$fb_redirect base_url() .'index.php/dashboard/';
  
  
$fb_data = array(
      
'me' => $profile,
      
'uid' => $user,
      
'loginUrl' => $this->facebook->getLoginUrl(
          array(
     
'scope' => $fb_scope,
                       
'redirect_uri' => $fb_redirect,
           )
       ),
    
'logoutUrl' => $this->facebook->getLogoutUrl(
       array(
                       
'next' => base_url() 
     )
       ),
    );

  
$this->session->set_userdata('fb_data'$fb_data); 
 Signature 

Curreri Damiano
Software and Web Developer
 
  Phone: +44 (020) 365 942 71
  Fax:    +44 (020) 365 942 71
 
      http://www.damianocurreri.co.uk
      .(JavaScript must be enabled to view this email address)

 
Posted: 14 November 2012 09:09 PM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2012-09-24
13 posts

I makes me mad

I use my daughter’s account and the projects works fine !!!!

but if I use my wife’s account not works

Why ?

 Signature 

Curreri Damiano
Software and Web Developer
 
  Phone: +44 (020) 365 942 71
  Fax:    +44 (020) 365 942 71
 
      http://www.damianocurreri.co.uk
      .(JavaScript must be enabled to view this email address)

 
Posted: 15 November 2012 03:18 AM   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-07
552 posts

Well, it goes wrong _some_where. Just start at the top and dump all that is returned. So when you do $user = $this->facebook->getUser(); do

$user $this->facebook->getUser();
echo 
'<pre>';var_dump(user); 

Etc. Good luck!

 Signature 

View my Tutsplus course ‘Build a CMS with Codeigniter’
http://codeigniter.tv/ - Codeigniter video tutorials
Video tutorial series on MY_Model