Jump to content

Call to undefined method Facebook::get_loggedin_user() - FBML


Recommended Posts

Hello,

 

I have no idea where to post this but basically I am trying to create a facebook application using a "Building Facebook Applications For Dummies" book but I am stuck on the first paragraph! This is the appinclude.php file:

 

<?php
require_once '../client/facebook.php';

// *** Add your Facebook API Key, Secret Key, and Callback URL here ***
$appapikey = '<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>';
$appsecret = '<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>';
$appcallbackurl = '<http://projectstratos.com/facebook/live/>';  

// Connect to Facebook, retrieve user 
$facebook = new Facebook($appapikey, $appsecret);
// $user = $facebook->require_login(); OLD 
$user = $facebook->get_loggedin_user(); 
$is_logged_out = !$user; 

// Exception handler for invalid session_keys 
try {
  // If app is not added, then attempt to add 
  if (!$facebook->api_client->users_isAppAdded()) {
    $facebook->redirect($facebook->get_add_url());
  }
} catch (Exception $ex) {
  // Clear out cookies for app and redirect user to a login prompt
$facebook->set_user(null, null);
$facebook->redirect($appcallbackurl);
}
?>

 

And this is the index.php file:

<h1>Watch Project Stratos Live!</h1>
<?php
require_once 'appinclude.php';

echo "<p>Your user id is: $user</p>";
echo "<p>Your name is: <fb:name uid=\"$user\" useyou\"false\" /></p>";

echo "<p>You have several freinds: </p>";
$friends = $facebook->app_client->friends_get();
echo "<ul>";
foreach ($friends as $friend) {
echo "<li><fb:name uid=\"$freind\" useyou\"false\" /></li>";
}
echo "</ul>";
?>

 

The facebook.php is the current fbml version (v2.1.2-4 I think) and the content in the h1 tag is echoed however after that there is the following errror message:

 

Fatal error: Call to undefined method Facebook::get_loggedin_user() in /xxxxxx/xxxxxx/xxxxxx/xxxx/xxxxxxxxxxxxxxxxxxxxxx/facebook/live/appinclude.php on line 12

 

I have mo idea what the problem could be! I have tried changing get_loggedin_user to getUser() but I just get another error, this is aparently (according to another forum) because I "forgot" to add get_loggedin_user.

 

Thanks in advance,

Cameron

I don't know how old the book is that you are trying to use, but the bad news is that the php facebook api has changed.  That method is no longer in the api, which is why you are getting the error. 

 

The new one is here:  https://github.com/facebook/php-sdk/

you would implement it something like this...

 

<?php
require_once '../client/facebook.php';

// *** Add your Facebook API Key, Secret Key, and enable cookies
$facebook = new Facebook(array(
  'appId'  => 'YOUR APP ID',
  'secret' => 'YOUR API SECRET',
  'cookie' => true, // enable optional cookie support
));
$session = $facebook->getSession();
// $user = $facebook->require_login(); OLD 
$logged_in = $facebook->getLoginUrl(); //login
$user = $facebook->getuser()
if($session) {
// Exception handler for invalid session_keys 
try {
  // If app is not added, then attempt to add 
  if (!$facebook->api_client->users_isAppAdded()) {
    $facebook->redirect($facebook->get_add_url());
  }
}} catch (Exception $ex) {
  //logout
$is_logged_out = getLogoutUrl(); //logout
}
?>

 

That fixed appinclude.php kind of... but now, in index.php, I have this code:

 

<h1>Watch Project Stratos Live!</h1>
<?php
require_once 'appinclude.php';

echo "<p>Your user id is: $user</p>";
echo "<p>Your name is: <fb:name uid=\"$user\" useyou\"false\" /></p>";

echo "<p>You have several freinds: </p>";
$friends = $facebook->app_client->friends_get();
echo "<ul>";
foreach ($friends as $friend) {
echo "<li><fb:name uid=\"$freind\" useyou\"false\" /></li>";
}
echo "</ul>";
?>

 

With this output:

 

Your user id is:

Your name is: Facebook User

You have several freinds:

Fatal error: Call to a member function friends_get() on a non-object in /hermes/bosweb/web035/b357/ipg.projectstratoscom/facebook/live/index.php on line 9

 

Why would this be?!

Hmm.. Thank you so much but this is getting ridiculous! I've got ANOTHER error:

 

 

Fatal error: Uncaught OAuthException: Invalid OAuth access token signature. thrown in /hermes/bosweb/web035/b357/ipg.projectstratoscom/facebook/client/facebook.php on line 543

 

Also, I am not getting  id or name, why would this be. Is there some sort of cheat sheet I can use to "convert" the old sdk to the new one? How simple would that be, is the basic idea the same?

I have used this for appinclude page but now that I want to list things, I will ask a third time, is there a cheat sheet that will actually convert from the V1 SDK to V2 functions?

 

I have not got this error:

 

Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in /hermes/bosweb/web035/b357/ipg.projectstratoscom/facebook/client/facebook.php on line 451

 

This is because I added this line:

$friends = $facebook->api('/me/friends');

 

This is the code that is causing the error on facebook.php:

 

    if (isset($result['error'])) {
      throw new FacebookApiException($result);
    }

 

where "throe new Facebook..." is the line 451.

 

Thanks

Cameron

you could use the FB class off of php classes. i am sure it is easier to figure out than the V1 stuff.

 

FB toolbox.class.php

<?php

/**
* Facebook Toolbox
*
* A helper class for facebook application development. Very handy for
* performing common facebook activities like:
*
* - Getting users profile information,
* - Getting friend list,
* - Getting application to user profile,
* - Sending email notification message
* - Sending Notification Message
* - Publishing news feed or story to user profile.
*
* @author      Raju Mazumder <rajuniit@gmail.com>
* @package     FBToolbox
* @copyright   2008-2009 Raju Mazumder
* @link        http://www.stylephp.com
* @since       Version 1.0
*/

// Include facebook client library
include_once ('client/facebook.php');

class FbToolbox
{
    /*
     * Holds the facebook object
     *
     * @var Object
     */
    private $facebook;

    /**
     * Facebook user id
     *
     * @var integer
     */
    private $fbuser;

    /**
     * Facebook API key
     *
     * @var string
     */
    private $apiKey;

    /**
     * Facebook secret key
     *
     * @var string
     */
    private $secretKey;

    /**
     * The constructor
     *
     * This function will initialize the class. You need to provide the API
     * key and the secret as argument
     *
     * @param string api key
     * @param string secret key
     * @return void
     *
     */
    public function __construct($apiKey, $secretKey)
    {
        $this->apiKey     = $apiKey;
        $this->secretKey = $secretKey;
        $this->facebook  = new Facebook($this->apiKey, $this->secretKey);

    }

    /**
     * Get User Info
     *
     * Retrieves the varous profile information for a given user. You can
     * provide the list of fields to retrieve. When nothing is specified,
     * it will fetch the basic ones.
     *
     * @param int facebook user id
     * @param string field list (optional)
     * @return array profile fields
     *
     */
    public function getUserInfo($fbuserId, $fields = "basic")
    {
        $this->fbuser = $fbuserId;

        if($fields == "basic")
        {
            $fields = array('first_name','last_name','profile_update_time','current_location', 'sex', 'birthday', 'pic_square');
        }

        $userDetails = $this->facebook->api_client->users_getInfo($this->fbuser, $fields);
        return $userDetails;
    }

    /**
     * Get Friend List
     *
     * This function will retrieve the friend list of any given facebook
     * user id. Optionally, it allows a few parameters to customize the
     * list.
     *
     * @param int facebook user id
     * @param bool whether the friends have installed this application
     * @param int start limit
     * @param int total limit
     * @return array friend list
     *
     */
    public function getFriendList($fbuserId, $appUser = false, $start = 0, $limit = 20)
    {
        $this->fbuser = $fbuserId;

        // Does the friends need to add the app to be qualified ? 
           if ($appUser == false)
           {
               $usersArray = $this->facebook->api_client->fql_query("SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = {$this->fbuser})");
               
           }
           else
           {
               $usersArray = $this->facebook->api_client->fql_query("SELECT uid FROM user WHERE has_added_app = 1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = {$this->fbuser})");
               
           }

           if (empty($usersArray))
           {
           return array();
           }

           // Make an array of the friends
           foreach ($usersArray as $user)
           {
               $users[] = $user['uid'];
           }

           // Put a limit of the friends if specified
           if ($appUser && !empty($users) && $limit)
           {
               $users = array_slice($users, $start, $limit);
           }

           // Return the friend list
           return $users;
    }

    /**
     * Add To Profile
     *
     * This function adds the application to specified user's profile.
     *
     * @param int facebook user id
     * @param string path to the screenshot or fbml of wider profile fbml which will add at box profile
     * @param string path to the screenshot or fbml of narrow profile fbml which will add at home page
     * @return void
     *
     */
    public function addToProfile($fbuserId, $wideprofileFbml, $narrowprofileFbml)
    {
        $this->fbuser = $fbuserId;
        $wide_handler = 'wide_handler_'.$this->fbuser;
        $narrow_handler = 'narrow_handler_'.$this->fbuser;
        $this->facebook->api_client->call_method('facebook.Fbml.setRefHandle', array('handle' => $wide_handler,
                                                                                     'fbml'   => $wideprofileFbml));

        $this->facebook->api_client->call_method('facebook.Fbml.setRefHandle', array('handle' => $narrow_handler,
                                                                                     'fbml'   => $narrowprofileFbml));

        $this->facebook->api_client->call_method('facebook.profile.setFBML',   array('uid'          => $this->fbuser,
                                                                                     'profile'      => '<fb:wide><fb:ref handle="'.$wide_handler.'" /></fb:wide>
                                                                                                        <fb:narrow><fb:ref handle="'.$narrow_handler.'" /></fb:narrow>',
                                                                                     'profile_main' => '<fb:ref handle="'.$narrow_handler.'" />'));
    }

    /**
     * Send Notification
     *
     * This function sends notification to the specified users.
     *
     * @param array    facebook user ids
     * @param string notification message
     * @param string notification type. can be user_to_user OR app_to_user
     * @return void
     *
     */
    public function sendNotification($ids, $msg, $notificationType = 'app_to_user')
    {
        $this->facebook->api_client->notifications_send($ids, $msg, $notificationType);
    }


    /**
     * Send Notification Email
     *
     * This function sends notification email to the specified users.
     *
     * @param string comma seprated facebook user ids
     * @param string subject of notification email
     * @param string notification message
     * @return void
     *
     */
    public function sendEmail($ids, $subject, $msg)
    {
        $this->facebook->api_client->notifications_sendEmail($ids, $subject, "", $msg);
    }

    /**
     * Publish News Feed
     *
     * This function will publish news feed to the specified user profile.
     *
     * @param int facebook user id
     * @param int template bundle id
     * @return void
     *
     */
    public function publishNewsFeed($fbuserID,$templateBundleId)
    {
        $this->fbuser = $fbuserID;
        
        $tokens  = array();
        $friends = $this->getFriendList($this->fbuser, true);
        $targets = implode(',', $friends);
        try {
            $this->facebook->api_client->feed_publishUserAction($templateBundleId, json_encode($tokens), $targets,'',3);
        }
        catch (Exception $ex) {
            //exception message
        }
    }
    /**
     * get template bundle id
     *
     * This function will get template bundle to register one story
     *
     * @param string one line story template
     * @return int template bundle id
     *
     */
    
    public function getTemplateBundleId($one_line_story_templates)
    {
        return $this->facebook->api_client->feed_registerTemplateBundle($one_line_story_templates); 
    }

} 

 

example.php

// Include our files
include_once ('FBToolbox.class.php');

// Prepare the object
$fbToolboxObj = new FBToolbox('YOUR_API_KEY', 'YOUR_SECRET_KEY');


// Get user information
$userInfo = $fbToolboxObj->getUserInfo('FB USER ID');
//print_r($userInfo);
echo $userInfo[0]['current_location']['city'];

// Get friend list here
$friendList = $fbToolboxObj->getFriendList('FB USER ID',false,0,20);
//print_r($friendList);

// Send notification
$fbToolboxObj->sendNotification(array('FB USER ID'),'test api class','app_to_user');

// Send email notification (CAUTION: your application must have permission from user)

$fbToolboxObj->sendEmail('FB USER ID','test api class','test api class');

// Publish news feed


$one_line_story_templates[] = '{*actor*} has developed a php wrapper 
class for facebook application developer.';
//You have to run this function only one times to get template bundle id
$templateBundleId = $fbToolboxObj->getTemplateBundleId($one_line_story_templates);

$fbToolboxObj->publishNewsFeed('FB USER ID',$templateBundleId);

// Add your application to your profile
$fbToolboxObj->addToProfile('FB USER ID',"Wider FbMl","narrow fbml");
?>

<!-- you have to print this line to add application to user profile
if user click on addtoprofile link then above function will work -->
<div class="section_button"><fb:add-section-button section="profile"/></div> 

  • 8 months later...

Sorry friends.... Im a newbie in PHP, but I need to learn more. With this script can I read the Facebook ID of a user with only enter to my fanpage? I need to say "Hello xxxxxxx" before she/he makes like to my fanpage.

 

Can I do that whit this?

 

Than You

Sorry friends.... Im a newbie in PHP, but I need to learn more. With this script can I read the Facebook ID of a user with only enter to my fanpage? I need to say "Hello xxxxxxx" before she/he makes like to my fanpage.

 

The dead should stay dead. Stop resurrecting Zombies!!!! Create your own damn thread.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.