Trouble getting User ID with getUser in PHP SDK

558 Views Asked by At

I am having a very hard time getting the Facebook PHP SDK to work with an Wordpress plugin within which I am trying to repair the login flow. The problem I am having is that getUser() always returns 0 even though there is definitely a user logged in. Basically, my code works as follows:

  1. I have an options page (within wordpress) that when it is loaded creates an instance of a wrapper class ror the PHP SDK Facebook class like this:

    $facebook = new FacebookAPI;
    

    The constructor for this wrapper creates an instance of the PHP SDK Facebook class and saves it, while also setting a few class variables and doing some work to manage sessions saved within the plugin.

  2. Then within the options page there is a button that is is a link to the login_url generated by the Facebook object, i.e. getLoginUrl(). The login Url is called this way, using the wrapper class:

    // Set Login Url
    $params = array(
        'scope' => 'user_photos,user_photo_video_tags',
        'redirect_uri' => FB_REDIRECT_URI,
        'display' => 'page'
    );
    $loginURL = $facebook->get_login_url($params);
    
  3. The redirect_uri here is the plugin options page within the wordpress and when the page is reloaded there is code that checks to see if the login process has completed okay. It does this by looking to see if the url contains a 'code' variable, i.e. if $_REQUEST['code'] is set. (This is what FB does when it sucessfully completes the login/permissions process.

The Problem: If the 'code' is set in the request object, I think I should then be able to call getUser() on the facebook sdk class and get the user id. This is where I run into the problem. getUser() always returns 0, and I haven't been able to figure out why.

One thing I have noticed, however, is that if I do var_dump($_SESSION), the array is empty. And in the PHP error log, I have the following PHP Warnings:

PHP Warning:  session_start(): Cannot send session cookie - headers already sent by (output started at /****/****/Sites/ld-full/wp-admin/menu-header.php:92) in /****/****/Sites/ld-full/wp-content/plugins/fotobook/facebook-platform/facebook.php on line 49
PHP Warning:  session_start(): Cannot send session cache limiter - headers already sent (output started at /****/****/Sites/ld-full/wp-admin/menu-header.php:92) in /****/****/Sites/ld-full/wp-content/plugins/fotobook/facebook-platform/facebook.php on line 49

Line 92 in menu-header.php, however, is as follows:

echo "\n\t<div class='wp-menu-image'><a href='{$submenu_items[0][2]}' aria-label='$aria_label'>$img</a></div>$arrow<a href='{$submenu_items[0][2]}'$class $aria_attributes>$title</a>";

I don't see anything here that would be writing headers. So I am at this point rather stumped.

Does anyone have any ideas about what might be going on here? Any help would be absolutely fantastic.

1

There are 1 best solutions below

3
On

The Problem: If the 'code' is set in the request object, I think I should then be able to call getUser() on the facebook sdk class and get the user id.

Nope. The code parameter’s purpose is to be exchanged for an access token.

there is code that checks to see if the login process has completed okay. It does this by looking to see if the url contains a 'code' variable, i.e. if $_REQUEST['code'] is set. (This is what FB does when it sucessfully completes the login/permissions process.

Getting the code parameter does not mean that authorization is already finished – the step mentioned above has to carried out first.

And in the PHP error log, I have the following PHP Warnings:

PHP Warning: session_start(): Cannot send session cookie - headers already sent by […]

Ppppleeeaaaaseee research that error message yourself – it’s been discussed and explained on the web more times than people have probably searched for Paris Hilton in an explicit context …