lisovaccaro
lisovaccaro

Reputation: 34016

Fetch user information with Facebook PHP api?

I just managed to connect to the facebook PHP api. Facebook API tutorial seems really bad or at least poorly organized.

I realised I can get the logged in user's name with: <?php print_r($user_profile[name]); ?> since it's beforehand set as $user_profile = $facebook->api('/me'); . How do I print another user's name who is not logged in, knowing his UID, for example '2222'

How do I fetch user info, specifically name and UID?

Thanks

Upvotes: 0

Views: 3643

Answers (3)

Jaime
Jaime

Reputation: 1410

In your api call, replace the '/me' with '/2222'

Upvotes: 0

Rukmi Patel
Rukmi Patel

Reputation: 2561

 <script>
          FB.init({appId: <?php echo FACEBOOK_APP_ID;?>, status: true, cookie: true, xfbml: true});
          FB.Event.subscribe('auth.logout', function(response) {});
          FB.Event.subscribe('auth.sessionChange', function(response) { 
            if (response.session) { 

              FB.api('/me', function(response) {             
                 if(response.id!='undefined')
                 {  
                    window.location='Fbaccess.php?first_name='+response.first_name+'&last_name='+response.last_name+'&email='+response.email;
                 }
                 else
                 {  
                    window.location='login.php';
                 }
                }); 

            } else {

              // The user has logged out, and the cookie has been cleared
            }
          });

        </script>

php code ....

<?php 
$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);        
if(isset($cookie))
{
    $first_name = json_decode(@file_get_contents('https://graph.facebook.com/me?access_token=' .$cookie['access_token']))->first_name;
    $last_name = json_decode(@file_get_contents('https://graph.facebook.com/me?access_token=' .$cookie['access_token']))->last_name;
} 
?>


function get_facebook_cookie($app_id, $application_secret) 
{
  $args = array();
  parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
  ksort($args);
  $payload = '';
  foreach ($args as $key => $value) 
  {
    if ($key != 'sig') {
      $payload .= $key . '=' . $value;
    }
  }
  if (md5($payload . $application_secret) != $args['sig']) {
    return null;
  }
  return $args;
}

i think this much code is available for you to get facebook name and user id .... on this url you will find whole response

https://graph.facebook.com/me?access_token=' .$cookie['access_token']

Upvotes: 0

saturngod
saturngod

Reputation: 24959

What user do you want to fetch ? You can fetch current user with /me. Before using the Facebook PHP API , you need to understand about Facebook graph api. It explain everything about Facebook api access url. So, you can call the graph URL in Facebook PHP SDK.

You can check sample source code from

https://github.com/facebook/php-sdk/

require './facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'YOUR_APP_ID',
  'secret' => 'YOUR_APP_SECRET',
));

// Get User ID
$user = $facebook->getUser();
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

Upvotes: 1

Related Questions