Emil Larsson
Emil Larsson

Reputation: 226

Get ID from Facebook PHP SDK

I've been sitting all night trying to figure out how to authorize a user via Facebooks API, just to get their User ID. I've tried almost all of the tips and ideas from S/O, 5 pages of "Google guides" and the Facebook Developer Guide but nothing seems to work for me! If anyone would be so kind and make a step-by-step guide how to get this to work!

Is Open Graph a must for this? I tried it aswell, didn't work with nor without it. I already have my App (maybe not the right settings there?) and the PHP SDK

( $uid = $facebook->getUser(); etc. not giving me any help at all. )

Thanks in advance!

This code for example is not working, I found it here on S/O and he said it was working flawlessly for him!

<?php
require_once 'facebook.php';
$facebook = new Facebook(array(
  'appId'  => '########',
  'secret' => '##########################',
  'cookie' => true,
));

$req_perms = "publish_stream,offline_access,user_status,email,read_stream"; 
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(array('canvas'=> 1,'fbconnect' => 0,'req_perms' => $req_perms));
$user_info = null;
if (!$session) 
    { echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";exit;}
else{
     try {  $user_info = $facebook->api('/me');  } 
     catch (FacebookApiException $e) 
         { echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>"; exit;}
    }
?>
<!doctype html>
<html>
<head>
</head>
<body>
<?php 
$user_name=$user_info['name'];
echo "Hi $user_name ! Your Facebook ID is: ".$user_info['id']."<br/>"; 
echo "Hi $user_name ! Your E-mail Address is: ".$user_info['email']."<br/>"; 
echo "Thanks for accepting our application.";
//print_r($user_info);
?>
</body>
</html>

Edit: Thanks for all the quick responses, I finally got it working! I was on the right track all along..

Step 1 : Make a link like this

https://www.facebook.com/dialog/oauth?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL

This authorizes the user with the facebook app, which is needed for step2.

Step 2 : Implement this code snippet.

require_once('facebook.php');

$fb_app_id = "APP_ID";
$fb_secret = "APP_SECRET";
$fb_app_url = "APP_URL";
$facebook = new Facebook(array(
    'appId'  => $fb_app_id,
    'secret' => $fb_secret,
    'cookie' => true,
));

$facebook_login_url = $facebook->getLoginUrl(array(
    'canvas' => 1,
    'fbconnect' => 0,
    'scope' => 'publish_stream,user_photos',
    'redirect_uri' => $fb_app_url
));

$facebook_user_id = $facebook->getUser();

Thank you @KevinCogill and @JuicyScripter for the tips!

Upvotes: 2

Views: 9643

Answers (2)

Juicy Scripter
Juicy Scripter

Reputation: 25938

Facebook already did it for you:

You must read Authentication and see examples

Update (notes on updated code sample):
You using getSession which isn't really exists in latest version of Facebook PHP-SDK and req_perms is called scope now. Did you read Facebook documentation or tutorials posted somewhere else? Facebook changing things frequently most of tutorials you'll find in other sources will be outdated very fast. Better to get info from official source (even if it's not the greatest quality)

Upvotes: 3

Skwerl
Skwerl

Reputation: 341

require_once('facebook/facebook.php');

$facebook = new Facebook(array(
    'appId'  => $fb_app_id,
    'secret' => $fb_secret,
    'cookie' => true,
));

$facebook_login_url = $facebook->getLoginUrl(array(
    'canvas' => 1,
    'fbconnect' => 0,
    'scope' => 'publish_stream,user_photos',
    'redirect_uri' => $fb_app_url
));

$facebook_user_id = $facebook->getUser();

Upvotes: 2

Related Questions