Facebook has announced and released a new PHP SDK to use on the platform, making significant changes to the way developers can access the API and the new Graph API 2.0. The new PHP SDK splits the functions into multiple classes, so you only need to include the classes you need.
In essence, making API calls and getting data has changed significantly. Just look at the sample code below which asks the user to login and prints their profile data:
You now need to include each individual class you require (
require_once), set the namespace alias (
use) and then calling the functions you need to achieve login and retrieval of API response.
The good thing is that the code can be reduced from the example above using chaining. From the example above, you can turn lines 92-95 into just one (large) line. Take this example:
The new SDK will definitely take a while to get used to. In fact, the new documentation already has a few errors in it that makes it difficult to even get started…
Additional Permissions / Scope
If your application needs additional permissions from the user, you can pass these in as an array to the
$helper->getLoginUrl() function. The first parameter takes an array of permissions like the example below:
Note: The Facebook PHP SDK 4.0.0 contains more classes than used in the example above, but I’ve included the minimum number needed to achieve login and retrieval of profile information.
For those asking about how to log a user out from your application, you must clear the session or delete any cookies you set. In my example, the following is required on the
logout.php page to clear the user’s session.
8th May 2014: The original code in this post has been updated to include session management. The new code now remembers any saved access_tokens, validates the token and makes an API call without asking the user to login again. The code works for the changes included in Facebook PHP SDK v4.0.1 and v4.0.2. The new code also includes an extra line to generate the logout link for the user.
You can see the changes to the code here.
21st May 2014: Facebook has yet again updated the Facebook PHP SDK, this time to version v4.0.5. The new changes are significant, adding a number of new files that are required for the Facebook SDK to work. I’ve updated my sample code yet again to account for these changes.
You can see the changes to the code here.
27th May 2014: Newly released version v4.0.6 continues to work with the above sample code.
10th June 2014: Facebook has updated the PHP SDK to version v4.0.8. The new changes mode some of the classes into a sub-folder, breaking existing code. The below snipped shows how the class includes and
use statements needs to be changes:
11th June 2014: New tutorial on how to create Facebook Tab Apps has been added. Required Facebook PHP SDK 4.0.7 or later.
27th June 2014: I’ve made a change to the initial code to deal with session errors. Essentially, I’ve changed the first <code>IF…ELSE</code> statement into two separate <code>IF</code> statements to deal with session errors, like expired sessions. You can see the changes separately here.
Facebook has also made further changes to the SDK but failed to tag the new version as v4.0.9. Therefore my modifications in Update 3 has been updated to include the further file changes in the same version (v4.0.8). You can see the modifications separately here.
27th October 2014: Added
logout.php code due to multiple requests.
One more question, if you have time. Why did you made $session 2x
First : $session = new FacebookSession( $_SESSION[‘fb_token’] );
Second : $session = new FacebookSession( $session->getToken() );
is there any a particular reason for that?
The first creates a new Facebook session from a access_token stored in a session. The second does the same thing, but using the Facebook session from redirect. Actually, there is no need for the second one, but I’ve put in there just in case. Ideally, by the time you get to the second, you should either have a session from FacebookRedirect or from the PHP $_SESSION.
Thank you for useful information.
Sir i am making a canvas application which requires.I am doing fine with the landing page but I am not sure how to proceed with the 2nd page .Can you please help me in this regard?
Sorry, I don’t provide specific coding help. You can use most of the code from this page to validate the access_token on the second page.
Sir, i have tried using the following code for making request to graph api form multiple page .Can you point out what I am missing on .Please help as I am novice to fb coding
$me = $response->getGraphObject();
You’re missing the entire
FacebookRequestpart, which calls the specific API. What API are you trying to call? Look at my example properly and copy it to achieve what you want.
I have an error “Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or ‘(‘ on line 23
And my line 23 I have this:
Can you help me please?
Are you using PHP 5.3.0 or newer? If not, you need to upgrade before the Facebook SDK will work. Otherwise, you’ll have to use the older SDK. The new SDK only works on newer versions of PHP.
Oh no 🙁 I have 5.2.17 version…so what SDK I have to use?
You need to use the Facebook PHP SDK from the Facebook Archive instead.
I updated my PHP version to 5.3.29 but now I have this error:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /web/htdocs/www.finsicilia.it/home/sdk/fb_4/src/Facebook/FacebookRedirectLoginHelper.php on line 191
The documentation for the Facebook PHP SDK v4.x says you need PHP 5.4+ for it to work. You should upgrade your version of PHP to the newest available one. PHP v5.3 is no longer supported – you should try and install PHP v5.5.
I don’t know why but $session is not set even after authorizing facebook from the login URL.Did i miss anything?
Check that your App ID and Secret are correct. If these are not correctly set then login won’t work.
Same problem here please any one let us know how to fixed it
many of thanks
Hi, I have a similiar question like Prashant Shaw. Perhaps you could help me.
I have a canvas page where I make:
$helper = new FacebookCanvasLoginHelper();
$session = $helper->getSession();
This is like I want, I get the userdata. Then I have a form with post to a second page, where I want to get the facebook data again – but here I cant get a session. I tried your code, but I dont now where is my error. Which url should I use for “FacebookRedirectLoginHelper” I tried the canvas-url, the app-url, the url where the script of the first page. Perhaps this is wrong.
Thank you very much for your hel!
FacebookCanvasLoginHelpershould only be used on the first page that’s loaded in the canvas, as this code uses the
signed_requestto authenticate the user. You should store the
signed_requestto a cookie or sessions and then just re-use it on other pages.
With facebook, as soon as you navigate to another page, the
signed_requestwill be lost, so it’s your responsibility to make sure you save the session and load it when required.
Thanks for your answer. I understand, that it was a wrong way from me. I will do it with a own session.
Hey marion ,
Did you proceed any further ?
I love you man.
So I’ve come back around and started over after using 1.0.4 successfully. However I’m having a problem.
The api forwards to the right page after logging into facebook but it doesn’t find the fb_token session for some reason on my site. There is a session that I can test for and verify but fb_token isn’t found. Suggestions?
The only thing I can think of is either the session isn’t set after authentication the user, or it’s being deleted / overwritten somewhere in the code.
Would session_regenerate_id() affect it? That occurs where FacebookRedirectLoginHelper() is redirected.
Yes, it could be that. It could be losing the session. Alternatively, try saving fb_token to a cookie instead of a session, and see if that helps? The cookie would remain on the system regardless of sessions.
Apparently my problems were due to my aggressive htaccess. I’ve found a way around it, thanks for your help.
[…] you post to Pages you administrate via the API. With the release of Graph API v2.x and the new Facebook PHP SDK v4.0.x, I’m updating my old tutorial to show how to achieve the same functionality using the newer […]
I wish Facebook API documentation would reflect these changes. Nothing works if you follow their “getting started” guide. They should call it a “getting stuck” guide.
Hi, thanks for the nice tutorial, And I follow your website every time
Greetings, I have a problem with the Facebook API latest version 4.5 ; I managed to set access token, app_id and app_secret all great to post on my site to the public, but the fact is that people who access my profile does not display the publication. I will miss that other settings?
Make sure your app is available to the public. If it’s in “sandbox” mode, other users won’t be able to see posts published by your app.