The way you create Facebook Page Applications has also changed when using the new Facebook PHP SDK v4.0.x. This tutorial shows you how to setup a basic Tab App using PHP SDK v4.0.7 or later. The
FacebookPageTabHelper class makes it easy to determine the page_id, adding in a like gate and checking if the user is an administrator.
Unfortunately, the new PHP SDK also doesn’t yet support the
app_data parameter you can pass to the Page URL. I’ve submitted a pull request to fix this issue, and awaiting Facebook to add it into the SDK.
Anyway, to create Page Apps, you need to make sure you include to additional classes from the SDK if they’re not already included:
- FacebookPageTabHelper.php (depends on the above)
These two classes will provide new functions to make retrieving a session from the Page Tab easier. Most of the code remains the same as my previous tutorial, but I’ve included the complete code below:
FacebookPageTabHelper depends on the
FacebookCanvasLoginHelper to provide functions to get the session from the Page or Canvas (wither via POST or GET). Using the helper, you can easily create a likegate using the
UPDATE #1 – July 2nd 2014
Facebook has updated the PHP SDK to v4.0.9, which has broken my original code. I have since updated the above code to work with the latest version of the SDK. You can see my revisions here. The new version introduced the file
FacebookSignedRequestFromInputHelper.php, which caused the previous code to break.
i got this error please any one help me
Fatal error: Class ‘Facebook\FacebookSignedRequestFromInputHelper’ not found in /app/Facebook/FacebookCanvasLoginHelper.php on line 33
Thanks for pointing out the issue. Facebook updated the SDK to v4.0.9, which caused my code to break. It has now been updated to work with the latest version.
its cannot working in localhost
You need to provide more information than that. Make sure the URLs are correct and you have configured your application correctly. If you don’t have SSL working on localhost, you should get a self-signed certificate setup.
I’m new to this and it looks like under 4.1 of SDK they have updated it again
Yes that’s correct. v4.1 is expected to have a lot of changes, and is still work in progress. I’ll be writing up new code for it once its stable. I recommend using the last stable release for now: v4.0.9.
Thank you Stepping back has at least meant I can get something going with you great code from above.
However everything works fine until I add in the line
I have it commented out right now and replaced with an echo
this can be seen here https://www.facebook.com/AvalancheAppsTest/app_819785261374364
When I put the line back I get an internal server error, sorry to be a nuisance, but I’m trying to learn this SDK.
Internal Server Errorprobably indicates that you have a PHP error in your code. Make sure the line is as follows:
From your comment, you may be using the incorrect code.
This is what I have
echo ‘getLoginUrl( array( ’email’, ‘user_friends’ ) ) . ‘” target=”_top”>Login‘;
Enable PHP errors on your site and see if it tells you what the problem is. You can achieve this by adding the following lines to the top of your code:
I had already done that still throws internal error 500. Even Log files shows the same thing.
Thank you very much, this has helped me a lot.
Could you tell us how to get other info, such as the users location and email address?
Also, what should be in redirect.php?
redirect.phpis really simple, and just has a redirect back to the main page of your app, e.g.:
Also, is there any point to having a login link in a tab app?
Is there no better way of doing the auth?
Hi Niraj …
Just an update, I got the login script going, in the latest version of PHP o our server some of the commands are deprecated and full error check was turned on, once I turned it off everything ran fine.
However I have another question, what is the nest way to deal with a situation where I want to remotely upload some content to page based on some other event, an example would be my client is running some special deal and weekly wants us to post to his page, but we want to have it that this is automatic and a script run it for several pages.
In other words the app needs to get authorisation but may not be logged in through a browser but run on a remote server. I hope that is clear.
One way or another, you’ll need to have a Facebook Login page to capture a valid access_token to call the API. This can be on any server, as long as the server running the automated script can later retrieve it. For this situation, I use a shared MySQL database between the two scripts. The FB Login page should write the access_token to the DB, and the other automated script would only need to fetch it.
So now how do you get the users email address and country?
/meendpoint and you’ll be able to see information about the user. If you requested the
user_locationpermissions, you’ll be able to see their email address and location.
I hope this is my last question, I have multiple pages in my app, so I tried this, I copied your code into index.php and Index2.php. I assumed once I authorize from index.php if I click a link to index2.php the code would display exactly the same as the app would pass the same data. It would appear not as all fields are blank, so how do I get the same pagehelper info in subsequent PHP files in the APP.
Any POST or GET data sent to
index.phpwill not transfer over to
index2.phpwhen you navigate. You’ll either have to pass this along manually (i.e. via the href in the link) or save the data to a session and use it from there. You can save the signed_request to a cookie or session and use it from there on subsequent pages.
Nice tutorial! It helped me to get startet with the new API.
I’m running this code (http://pastebin.com/vHhVsGz7) and it returns me an error:
“(#100) The ‘input_token’ parameter is required’ in …”
The print_r shows me the token, but looks like it can use it on the validation;
In advantage, why can’t I set a new FacebookPageTabHelper on another page, just in the first loaded by the pagetab?
If you are attempting to validate the session, you should try something like:
isLiked is going to be canceled …
Yes it is. Please read about it here.
it showing me blank page and even not showing me error.. 🙁
Enable errors in your code to see if PHP is throwing any warnings or errors. Also, make sure the URLs used in the code are updated to match the URLs for your host / environment.
I added at top as you suggest
but still nothing happen even not creating error log file .. when i add these line on another page it generate there..
and also when i run exact same code on localhost xampp its working there.. help me to figure out actual problem
Have you double checked your FB App settings and the URLs. You may need to add a / to the end of your Canvas URL.
yup i did … now i am trying to update php version(5.2) to latest .. lets see whats happen…
Thank you, for such a nice post.. it helped alot
Finally, which PHP version we must use and which version of Facebook PHP SDK?
The code in article doesn’t work for me?
Is necessary to get ‘user_likes’ permission?
If anyone knows please inform me. Thanks in advance
You should use the latest stable version of the Facebook PHP SDK, which is currently 4.0.10. Do not use v4.1 as this is currently unstable. The
$pageHelper->isLiked()won’t work anymore as Facebook is removing this functionality for new apps, and old apps from November 5th 2014. Read about it here.
Hello. Thanks for your great tutorials. I used to be able to pull my FB page events into my sites. But with the new sdk 4.* I can’t seem to get things to work. Do you have any pointers? Seems like it should be so simple …
Nice share, I was troubleshooting the signed request over past 2 hours, finally got something to work on with.
Keep it up
Thank you for this marvellous tutorial. It got me started, and I’ve been able to write my apps. Now I am ready to install my app on a page tab, and I want to do one thing more:
I want the app to identify the page it is running on, so I don’t have to write a separate app for each page. And I cannot get past this:
[appId:protected] => xxxxx
[appSecret:protected] => xxxxxx
Has facebook stopped letting us have page id, or am I missing something.
Thanks very much.
You can only identify the Page ID your app is running on first load of the app. On subsequent page requests, the signed_request is no longer sent. If you use the code in this tutorial, the Page ID is correctly returned and nothing has changed on Facebook’s side. Use Line 68 to get the Page ID.