zmuci
zmuci

Reputation: 538

Retrive Facebook username or user-id with PHP

Is there a way I can retrive users id? I know how get id from getGraphObjec but that id is not the id I want. I found something like http://graph.facebook.com/userid. I want that id, for example I generate "10205575645933169" from getGraphObject, but I want "1313375997" which is used in http://graph.facebook.com/1313375997. I don't even know if that is possible, but if it is, does anyone know a way to do it? Thank you

<?php

session_start();

require_once 'vendor/autoload.php';

Facebook\FacebookSession::setDefaultApplication('856997051006897','8e034c9c2bb159f9eba8f251dcc68eba');
$facebook = new Facebook\FacebookRedirectLoginHelper('http://localhost/FacebookLogin/index.php');

try {
	if($session = $facebook->getSessionFromRedirect()){
		$_SESSION['facebook'] = $session->getToken();
		
		header('Location: index.php');
	}
	
	if(isset($_SESSION['facebook'])) {
		$session = new Facebook\FacebookSession($_SESSION['facebook']);
		$request = new Facebook\FacebookRequest($session, 'GET', '/me');
		$request = $request->execute();
		
		$user = $request->getGraphObject()->asArray();		
		}	
} catch(Facebook\FacebookRequestException $e) {
	// Facebook error
} catch(Exception $e) {
	// local error
}

And I call it with $user['id']; in my code.

Upvotes: 0

Views: 537

Answers (2)

andyrandy
andyrandy

Reputation: 73984

Since v2.0 it´s not possible to retrieve the "username" or the "real/global ID" anymore. You will only get the real name of the user and an App Scoped ID. With the username, you could get the "real" ID of a user, so the whole concept with the App Scoped IDs would be void if it would still be possible to get the username or the real/global ID.

Check out the changelog for more information about recent changes: https://developers.facebook.com/docs/apps/changelog

Upvotes: 1

m_pro_m
m_pro_m

Reputation: 340

Graph API returns APP SCOPED USER ID. You can work with that ID in your code. It's still the same user. You can do requests like

$request = new Facebook\FacebookRequest($session, 'GET', '/'.$app_scoped_user_id);

I used to have the same problem. You can check it here Graph API and PHP SDK returns different results

Upvotes: 0

Related Questions