Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Facebook API: Creating Page Apps using Facebook PHP SDK 4.0.x

Facebook API: Creating Page Apps using Facebook PHP SDK 4.0.x

39

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:

  • FacebookCanvasLoginHelper.php
  • 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:

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.

Facebook, Facebook Graph API, Facebook PHP SDK 4.0, Fan Page, PHP

39 comments on “Facebook API: Creating Page Apps using Facebook PHP SDK 4.0.x”

  1. anandan says:
    July 1, 2014 at 2:41 PM

    i got this error please any one help me

    Fatal error: Class ‘Facebook\FacebookSignedRequestFromInputHelper’ not found in /app/Facebook/FacebookCanvasLoginHelper.php on line 33

    Reply
    • Niraj Shah says:
      July 2, 2014 at 11:23 AM

      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.

      Reply
    • anandan says:
      July 21, 2014 at 10:54 AM

      its cannot working in localhost

      Reply
      • Niraj Shah says:
        July 21, 2014 at 10:01 PM

        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.

  2. Rob Parker says:
    July 7, 2014 at 9:02 PM

    Hi there

    I’m new to this and it looks like under 4.1 of SDK they have updated it again

    Reply
    • Niraj Shah says:
      July 7, 2014 at 9:43 PM

      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.

      Reply
      • Rob Parker says:
        July 8, 2014 at 4:42 PM

        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

        echo '<a>getLoginUrl( array( 'email', 'user_friends' ) ) . '" target="_top">Login</a>';

        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.

      • Niraj Shah says:
        July 8, 2014 at 6:15 PM

        The Internal Server Error probably indicates that you have a PHP error in your code. Make sure the line is as follows:

        echo '<a href="' . $helper->getLoginUrl( array( 'email', 'user_friends' ) ) . '" target="_top">Login<a>';

        From your comment, you may be using the incorrect code.

  3. Rob Parker says:
    July 8, 2014 at 6:30 PM

    This is what I have

    echo ‘<a>getLoginUrl( array( ’email’, ‘user_friends’ ) ) . ‘” target=”_top”>Login</a>’;

    Reply
    • Niraj Shah says:
      July 8, 2014 at 6:33 PM

      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);
      

      Reply
      • Rob Parker says:
        July 8, 2014 at 6:37 PM

        I had already done that still throws internal error 500. Even Log files shows the same thing.

  4. Michael says:
    July 15, 2014 at 12:28 PM

    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

    Reply
  5. Michael says:
    July 15, 2014 at 12:39 PM

    Also, what should be in redirect.php?

    Reply
    • Niraj Shah says:
      July 15, 2014 at 2:30 PM

      The redirect.php is really simple, and just has a redirect back to the main page of your app, e.g.:

      
      

      Reply
      • Michael says:
        July 15, 2014 at 3:24 PM

        Thank you.

        Also, is there any point to having a login link in a tab app?

        echo '<a href="' . getLoginUrl( array( 'email', 'user_friends' ) ) . '" target="_top">Login</a>';

        Is there no better way of doing the auth?

      • Niraj Shah says:
        July 15, 2014 at 3:38 PM

        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.

  6. Rob Parker says:
    July 15, 2014 at 4:56 PM

    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

    Reply
    • Niraj Shah says:
      July 16, 2014 at 11:01 AM

      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.

      Reply
  7. Michael says:
    July 16, 2014 at 8:53 AM

    Thank you, a bit of JavaScript did the trick.

    So now how do you get the users email address and country?

    Thanks.

    Reply
    • Niraj Shah says:
      July 16, 2014 at 11:03 AM

      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.

      Reply
  8. Rob Parker says:
    July 16, 2014 at 6:18 PM

    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.

    Reply
    • Niraj Shah says:
      July 18, 2014 at 9:53 AM

      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.

      Reply
  9. Guilherme Redü says:
    July 22, 2014 at 2:53 PM

    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;

    Reply
    • Guilherme Redü says:
      July 22, 2014 at 3:16 PM

      In advantage, why can’t I set a new FacebookPageTabHelper on another page, just in the first loaded by the pagetab?

      Reply
    • Niraj Shah says:
      July 22, 2014 at 3:23 PM

      If you are attempting to validate the session, you should try something like:

      try {
          if ( !$session->validate() ) {
            $session = null;
          }
        } catch ( Exception $e ) {
          // catch any exceptions
          $session = null;
        }
      

      Reply
  10. jernejr says:
    August 12, 2014 at 8:09 PM

    isLiked is going to be canceled …

    Reply
    • Niraj Shah says:
      August 13, 2014 at 6:21 PM

      Yes it is. Please read about it here.

      Reply
  11. Abdul says:
    August 19, 2014 at 7:24 AM

    it showing me blank page and even not showing me error.. 🙁

    Reply
    • Niraj Shah says:
      August 19, 2014 at 10:21 AM

      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.

      Reply
      • Abdul says:
        August 19, 2014 at 11:38 AM

        I added at top as you suggest

        error_reporting(E_ALL);
        ini_set(‘display_errors’, 1);

        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

      • Niraj Shah says:
        August 19, 2014 at 1:04 PM

        Have you double checked your FB App settings and the URLs. You may need to add a / to the end of your Canvas URL.

  12. Abdul says:
    August 19, 2014 at 3:03 PM

    yup i did … now i am trying to update php version(5.2) to latest .. lets see whats happen…

    Reply
  13. alikhan8901 says:
    August 21, 2014 at 2:10 PM

    Thank you, for such a nice post.. it helped alot

    Reply
  14. gnsnomos says:
    August 25, 2014 at 10:29 AM

    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

    Reply
    • Niraj Shah says:
      August 27, 2014 at 11:13 AM

      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.

      Reply
  15. Jim says:
    September 3, 2014 at 8:56 PM

    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 …

    Reply
  16. askwhyweb says:
    November 28, 2014 at 7:39 AM

    Nice share, I was troubleshooting the signed request over past 2 hours, finally got something to work on with.

    Keep it up

    Reply
  17. Nancy Knox says:
    December 30, 2014 at 12:12 AM

    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:
    FacebookFacebookPageTabHelper Object
    (
    [pageData:protected] =>
    [signedRequest:protected] =>
    [appId:protected] => xxxxx
    [appSecret:protected] => xxxxxx
    [state] =>
    )
    Has facebook stopped letting us have page id, or am I missing something.
    Thanks very much.

    Reply
    • Niraj Shah says:
      December 30, 2014 at 12:11 PM

      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.

      Reply

Leave a ReplyCancel reply

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

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bash Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Flash Geolocation Google Nexus 5 Hacking HTML5 Input Sanitization iOS JavaScript jQuery Laravel 5 Laravel 5.2 Linux Mac OS NodeJS Parse PDF PHP Plugin Portfolio Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy