Asked  1 Year ago    Answers:  5   Viewed   8 times

I'm trying to use the Graph API and the PHP SDK for Facebook to try and return the current users name, but I'm only having luck with it displaying my name. This is pretty straight forward and someone that knows the Graph API shouldn't have any problem helping me out just by looking at the code I'm using.

require 'facebook.php';

$facebook = new Facebook(array(
  'appId'  => $appid,
  'secret' => $appsec,
  'cookie' => true
$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) {
    $user = null;

And then I use this: echo $user_profile['name']; to display the users name, but it is only displaying my name. On any other profile it just shows where that variable is as blank. On my profile, it shows my name. I also used print_r($user); and it is displaying my ID, but for any other profile it displays 0.

Do users need to authenticate themselves or should there already be a session with this info? How do I get that session info if there is one?

Anyone who can help out I would be very grateful. Thanks.



For security and privacy as well, Facebook users must login and allow the current application to have access to their account information. You can accomplish this using different SDK. Most people use Javascript but I persoally prefer PHP. As simple implementation in HTML is shown below:

<a href="<?php echo $this->facebook->getLoginUrl(array('next' => "", 'req_perms' => 'email,read_stream,publish_stream,offline_access'))?>">Login to Facebook</a>

It will generate something like:

<a href="">Login to Facebook</a>

you should be able to access the user's information after that process using Graph API.

$fbuser = $facebook->api('/me');
echo "Hello ".$fbuser['name'];

Let me know if works for you.

Saturday, May 29, 2021

To get the user access token in PHP SDK 3.1.1:

// Get the current access token
$access_token = $facebook->getAccessToken();

Refer to:

Posting Actions.

Refer to:

This tutorial will guide you through the key steps to build, test, and publish your first Open Graph application. We will build a sample recipe application that allows users to publish stories about cooking recipes. Before starting, please review the Open Graph Checklist which will not only help in your app design and planning, but will also help speed up the application review process.

Step 1: Create a Facebook applications.

Step 2: Authenticate users with the Login Button plugin.

Step 3: Define Objects, Actions and Aggregations through the App Dashboard.

Step 4: Publish Actions for your users.

Step 5: Add Social Plugins to your application.

Step 6: Submit Your Actions for approval.


if ($user){
    $queries = array(
        // The URL build is me/ namespace : action ? object = URL
        array('method' => 'POST', 'relative_url' => '/me/anotherfeed:view?feed=')
        // Any other API calls needed, this is a batch request for performance.

    try {
        $postResponseA = $facebook->api('?batch='.json_encode($queries), 'POST');
    catch (FacebookApiException $e) {
        //echo 'AF error: '.$e.'';
    // Returns the id of posted actions if true.
    $actions = json_decode($postResponseA[0][body], true);
Thursday, April 1, 2021

solution was to put all the code into one index.php file

Thursday, April 1, 2021

Are you sure you want to only logout of your app, and not fb too, if you logged in using fb? What kind of app could want to do something like that? And why? What's the benefit of doing so?

But if you really want to (hack), in index.php:

require 'init.php';    // general init
require 'init_fb.php'; // sets $fb_id if logged in to fb

if (isset($fb_id) and !isset($_SESSION['do_not_auto_login'])) {
    echo "<a href='/logout'>Logout</button>";
    include 'app.php';

if (isset($_SESSION['do_not_auto_login'])
     echo "<a href='/re_login'>FB Login</button>";
else include 'html_and_js_fb_login_code_and_button.php';

include 'startpage.php';

and in logout.php:

$_SESSION['do_not_auto_login'] = true;
// destroy-code

and in re_login.php:

header("location: /"); // re-direct back to index.php

Not perfect, but works somewhat good. Hope you get the hack!

But still, why?? If you don't think your user would like to come back, ask "How to remove my fb-app from user?" or something instead or try @DMCS's answer.. Why?

Saturday, May 29, 2021

You can. It is under the /user_id?fields=inbox

You will need read_mailbox (extended permissions) permission too.

Saturday, May 29, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :