Symfony2 and the Facebook php sdk 4

To work with the Facebook PHP SDK 4 in Symfony2, the first step is to add it to the composer. Edit the composer.json require and add this line:

"facebook/php-sdk-v4" : "4.0.*",

and then do an update (“php composer.phar update”). If it worked, the classes can be included via “use” in your controller:

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;

The classes can now be accessed without a path, like this:

FacebookSession::setDefaultApplication($id, $secret);

Symfony doesn’t like how facebook is handling the session, so sooner or later there will be an exception:

FacebookSDKException: Session not active, could not store state.

This can easily be fixed, since a session_state check is not necessary:

$helper = new FacebookRedirectLoginHelper( $this->generateUrl( 'facebooklogin', array(), true ) );
$helper->disableSessionStatusCheck();

Thanks a lot to the Stackoverflow Entry by SammyK, who presents all 3 options clearly

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.