Facebook API: Creating Page Apps using Facebook PHP SDK 4.0.x
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:
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:
The new 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 $pageHelper->isLiked() function:
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.
39 comments on “ Facebook API: Creating Page Apps using Facebook PHP SDK 4.0.x ”
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.
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.
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.
The Internal Server Error probably indicates that you have a PHP error in your code. Make sure the line is as follows:
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:
error_reporting(E_ALL); ini_set('display_errors', 1);
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? Thank You
The login link is there as an example. You can always log the user in by redirecting them straight to login URL using JavaScript. But, the link is still the best way to log a user into a Page tab. I tend to put a login link or button on my Page Tab apps, usually behind a like-gate, so users know what to expect.
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. Thanks
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.
Thank you, a bit of JavaScript did the trick. So now how do you get the users email address and country? Thanks.
Call the /me endpoint and you’ll be able to see information about the user. If you requested the user_email and user_location permissions, 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.php will not transfer over to index2.php when 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?
try < if ( !$session->validate() ) < $session = null; >> catch ( Exception $e ) < // catch any exceptions $session = null; >
Using Facebook API to post articles from PHP
Step 1.
Create a developer account from developer.facebook.com. All you need is your facebook account and you can get a developer account from it.
Step 2.
Create a Facebook page. From your Facebook page, on the top right corner, click on the dropdown menu and hit on create page. Select the type of the page you would like to create and then you have it.
Now what you want to do is, to create an auto post to the facebook page you just created from PHP. Lets say you run a blog or product page and you want to post whenever there is new stuff to be posted to the facebook page automatically.
Download Facebook PHP API from https://github.com/facebook/php-graph-sdk. You can either clone it using git if you have git already or just download the zip file and extract it to the directory of your webserver.
Step 4.
Create the following Class you will be using to post to Facebook
/** * Script for posting the videos to FB automatically. * @author Kaleb */ define ('FACEBOOK_SDK_V5_SRC_DIR', __DIR__.'/php-graph-sdk/src/Facebook/'); require_once(__DIR__.'/php-graph-sdk/src/Facebook/autoload.php'); class FacebookPoster < private $facebook; public function __construct(array $facebook_data) < $this->facebook = new Facebook\Facebook($facebook_data); > /** * Post to facebook page using the given values. * */ public function post(array $post_data, $token) < if (!empty($post_data)) < try < $post_response = $this->facebook->post('/me/feed', $post_data, $token); > catch (Facebook\Exceptions\FacebookSDKException $ex) < // $logger->log($ex->getMessage(); echo "Error occurred " . $ex->getMessage(); > > > >
To run the above class you will use the following
$facebook_data = [ 'app_id' => ‘APP_ID_GOES_HERE’, 'app_secret' => ‘APP_SECRET_GOES_HERE’, 'default_graph_version' => 'v2.2' ]; $facebook_poster = new FacebookPoster($facebook_data); $post_data = [ 'link' => 'YOUR LINK YOU WANT ON FACEBOOK', 'message' => 'YOUR MESSAGE YOU WANT ON THE POST' ]; $token =‘YOUR_TOKEN_GOES_HERE’; //post it. $facebook_poster->post($post_data, $token);
Don’t worry about the app_id, app_secret and token for now. Those are the things you will get from facecbook and plug them in later.
Step 7.
Now create an application in Facebook
For this, got to https://developers.facebook.com and on the right top corner you will get the dropdown and in there you will see create app. Follow the wizard and you will create the application.
Go to https://developers.facebook.com/apps/ to see the app, click on it and you will see the app_id and app_secret there. Those are what will be needed by step6.
Now you need to hook the application with the facebook page.
Step 9.
Go to https://developers.facebook.com/tools/explorer/ and click the app you created from Graph API explorer
Then select user token from the get token part. Select public page and manage page for permission.
Step 11.
From the Get Token, select the facebook app you have created earlier. You will get the popup to assign the permission and just follow the wizard.
Step 12.
Go to the facebook page you created and go to the settings, on the left you will see apps. Click on the apps and search for the app you created. You will get permission stuff and just complete that.
Step 13.
Go back to the explorer page https://developers.facebook.com/tools/explorer/ and select the application from the dropdown and also select the facebook page from Get Token. Now pick the token and that will be the last part to be inserted as token on Step 6.
Now you have everything to crank your engine. Let me know if you got problem on this.