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';

$appid=get_post_meta($thisID,'_EFBPDAppID',true);
$appsec=get_post_meta($thisID,'_EFBPDAppSec',true);
$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) {
    error_log($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.

 Answers

2

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' => "http://www.yourdomain.com/facebooklogin_success", 'req_perms' => 'email,read_stream,publish_stream,offline_access'))?>">Login to Facebook</a>

It will generate something like:

<a href="https://www.facebook.com/login.php?api_key=1234567890&cancel_url=http%3A%2F%2Fyourdomain.com/facebooklogin_cancel.php%2F&display=page&fbconnect=1&next=http%3A%2F%2Fyourdomain.com/facebooklogin_success.php&return_session=1&session_version=3&v=1.0&req_perms=email%2Cread_stream%2Cpublish_stream%2Coffline_access">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
 
5

To get the user access token in PHP SDK 3.1.1:

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

Refer to: https://developers.facebook.com/docs/reference/php/facebook-getAccessToken/


Posting Actions.

Refer to: https://developers.facebook.com/docs/opengraph/tutorial/

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. https://developers.facebook.com/docs/opengraph/tutorial/#create-app

Step 2: Authenticate users with the Login Button plugin. https://developers.facebook.com/docs/opengraph/tutorial/#authenticate

Step 3: Define Objects, Actions and Aggregations through the App Dashboard. https://developers.facebook.com/docs/opengraph/tutorial/#define

Step 4: Publish Actions for your users. https://developers.facebook.com/docs/opengraph/tutorial/#publish

Step 5: Add Social Plugins to your application. https://developers.facebook.com/docs/opengraph/tutorial/#plugins

Step 6: Submit Your Actions for approval. https://developers.facebook.com/docs/opengraph/tutorial/#submit

EXAMPLE:

if ($user){
    $queries = array(
        // The URL build is me/ namespace : action ? object = URL
        array('method' => 'POST', 'relative_url' => '/me/anotherfeed:view?feed=http://anotherfeed.com/')
        // 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
 
4

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

Thursday, April 1, 2021
4

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:

<?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';
    return;
}

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:

unset($_SESSION['do_not_auto_login']);
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
 
Hat
 
Hat
4

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 :