Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm writing a plugin for Wordpress that required the Facebook PHP SDK. I've run into what I think is a common problem judging from the number of results I've found dating back to over a year ago. Unfortunately, I have been unable to find a solution.

I'm using Wordpress 3.8 and Facebook PHP SDK Master taken from github so this should be the latest version. I've actually run this same code outside of Wordpress and it successfully ran with no errors.

So, i upload my plugin and activate it. I go to the panel I've built that asks for the appID and Secret key and also the username for the facebook account. All the plugin will do is grab the username me feed.

So, when i try to run the primary function that loads the feed I get the following errors:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at {path}/wp-admin/includes/template.php:1698) in {path}/wp-content/plugins/{mypluginname}/facebook-php-sdk/src/facebook.php on line 48

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at {path}/wp-admin/includes/template.php:1698) in {path}/wp-content/plugins/{mypluginname}/facebook-php-sdk/src/facebook.php on line 48

What I've tried

So, I've read in a few different posts that I should use ob_start() and ob_end_flush() php functions and that will fix the problem. As i understand the problem I am having I am sending things to the header where I shouldnt because the header has already been sent.

I've tried ob_start / ob_end_flush at the top and bottom of my plugin and it made no different.

I've tried downloading the Facebook wordpress plugin and using the modified Facebook PHP SDK that that plugin uses and that got rid of those errors but created a new error.

Fatal error: Access to undeclared static property: WP_BaseFacebook::$CURL_OPTS in {path}/wp-content/plugins/{pluginname}/facebook-php-sdk/src/base_facebook.php on line 932

So, I read that this is because CURL is not enabled. I've checked my PHP INFO page and CURL is enabled. I use curl requests in another plugin. I do see certain things under the CURL features that are not enabled. Does anyone know which option I need to enable to get this modified version of the Facebook PHP SDK to work with my plugin?

Does anyone know how to fix either the errors I am getting from the standard Facebook PHP SDK OR does someone know how to fix the errors I am getting from modified Facebook PHP SDK?

Thanks in advance. I hope to get a solution to both worked and then finally put this issue to rest. We've been plagued with this same problem since 2011.

In order to provide some more details, here is part of the code I have in my plugin.

require_once('facebook-php-sdk/src/facebook.php');

global $facebook; // read that i needed to add this line to my script; it didnt help.

// Create our Application instance (replace this with your appId and secret).
$fb_config = array(
  'appId'  => $ijFaceFeedAppID,
  'secret' => $ijFaceFeedSecretKey,
  'fileUpload' => false,
  'allowSignedRequest' => false
);

// get feed
$facebook = new WP_Facebook($fb_config); // am using the modified facebook sdk
$page_feed = $facebook->api(
'/'.$ijFaceFeedUser.'/feed',
'GET'
);
share|improve this question
    
The Facebook PHP SDK is a crap. Use the javascript version. –  Chris_O Dec 14 '13 at 11:06
    
I'm not sure if i can use javascript for what i am trying to do. I'm pulling the feed in and them importing it into a database and then i use a wordpress shortcode to display the message on my website. I can see there are issue with the Facebook PHP SDK. Hopefully they improve it. –  Robbiegod Dec 14 '13 at 16:30
    
As-written, this question involves PHP server configuration and a non-WordPress-specific PHP script, and is therefore off topic for WPSE. Please edit your question to clarify how it is specific to WordPress. –  Chip Bennett Dec 18 '13 at 19:31
    
I'm writing a wordpress plugin that uses the Facebook PHP SDK. I'm running into the problem only when I put the Facebook PHP SDK into my Wordpress plugin. The code runs fine outside of the plugin as i have tested it. I believe I am correct in asking my question in the Wordpress section since the problem only occurs when it is wrapped into a wordpress plugin that I am writing. I was hoping that maybe other who are authoring Wordpress plugins would be able to offer some help getting the Facebook PHP SDK to work inside of a wordpress plugin. I think i make that clear in the question. Thanks. –  Robbiegod Dec 19 '13 at 14:26

2 Answers 2

up vote 1 down vote accepted

First of all download the zip of facebook php sdk from Facebook PHP SDK from Github.com

Extract the zip folder, you need only the 'src' folder for integrate your facebook application in your plugin.

copy the 'src' folder and paste it in your plugin directory.

In your plugin installer file, first start your session by writting session_start() and then include the facebook.php file from src folder. Like this:

session_start(); include('src/facebook.php');

Now follow the below code.

session_start();
include('src/facebook.php');

$config = array('appId' => 'Your APP ID', 'secret'=> 'Your App Secret');

$connect = new Facebook($config);

$user = $connect->getUser();
$me = null;

if($user)
{
    try
    {
         $me = $connect->api('/me');
    }
    catch(FacebookApiException $e)
    {
        echo $e->getMessage();
    }
}

if($me)
{
    $logoutUrl = $connect->getLogoutUrl();
    echo "Logout";
}
else
{
    //as the facebook php sdk version is 3 so there is some change for permissions
    // If you want to add permission for read_streams so that pass array with login url
    $params = array('scope'=>'read_stream');
    $loginUrl = $connect->getLoginUrl($params);
    echo "Login";
}

Now you can login with facebook and asks for permission for read stream for first time only.

share|improve this answer
    
ok, thank you for your answer. I will try this out. I might have been missing the session_start(); line in the plugin. –  Robbiegod Dec 19 '13 at 14:29
    
I tried your complete code in my plugin but i am still getting errors. –  Robbiegod Dec 19 '13 at 15:59
    
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent Is there a special place or function that i need to put the facebook sdk calls inside of my plugin? Currently, I have a function setup to get the "me" feed from Facebook. I'm adding that call as a link in the admin left menu so when I click that link from the wordpress menu it tried to reach out the facebook feed. I'm getting these header errors. –  Robbiegod Dec 19 '13 at 16:04
    
i just tried putting session_start() as the first line of my plugin, that removed the error. I think that may have solved the header error problems. Question now is is that the best place in the plugin to put the session_start() function? –  Robbiegod Dec 19 '13 at 16:08

Special Thanks @Azhar Munshi very much I faced the same problem but now after I've Tried to put session_start(); before include('src/facebook.php'); the errors disappear :)

share|improve this answer
    
use ob_start() next to session_start() –  balamurugan Dec 30 '13 at 8:06
    
i did not need to put ob_start() before session_start() and it seems to work just fine. I had seen others using ob_start() too, but can u elaborate why that is the better option vs. just using session_start() by itself? –  Robbiegod Dec 30 '13 at 21:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.