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: PHP SDK Updated to v4.0.0

Facebook API: PHP SDK Updated to v4.0.0

291

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.

Basic Example

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.

Chaining

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.

Logout Page

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.


Update #1

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.


Update #2

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.


Update #3

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.


Update #4

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.


Update #5

27th October 2014: Added logout.php code due to multiple requests.

Facebook, Facebook Graph API, Facebook PHP SDK 4.0, PHP

291 comments on “Facebook API: PHP SDK Updated to v4.0.0”

  1. Predrag Rodić says:
    February 23, 2015 at 10:25 AM

    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?

    Reply
    • Niraj Shah says:
      February 23, 2015 at 9:41 PM

      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.

      Reply
      • Predrag Rodić says:
        February 24, 2015 at 11:09 AM

        Thank you for useful information.

  2. Prashant Shaw says:
    February 27, 2015 at 9:16 AM

    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?

    Reply
    • Niraj Shah says:
      February 27, 2015 at 9:52 AM

      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.

      Reply
      • Prashant says:
        April 15, 2015 at 10:55 AM

        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

        execute();
        $me = $response->getGraphObject();

        }

        ?>

      • Niraj Shah says:
        April 17, 2015 at 9:03 AM

        You’re missing the entire FacebookRequest part, 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.

  3. Luca Pandolfo says:
    February 28, 2015 at 11:24 AM

    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:
    use FacebookHttpClientsFacebookHttpable;
    ….

    Can you help me please?

    Reply
    • Niraj Shah says:
      February 28, 2015 at 12:30 PM

      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.

      Reply
      • Luca Pandolfo says:
        February 28, 2015 at 1:23 PM

        Oh no 🙁 I have 5.2.17 version…so what SDK I have to use?

      • Niraj Shah says:
        February 28, 2015 at 10:24 PM

        You need to use the Facebook PHP SDK from the Facebook Archive instead.

      • Luca Pandolfo says:
        March 2, 2015 at 5:20 PM

        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

      • Niraj Shah says:
        March 3, 2015 at 10:03 AM

        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.

  4. bravokeyl says:
    April 3, 2015 at 11:57 PM

    I don’t know why but $session is not set even after authorizing facebook from the login URL.Did i miss anything?

    Reply
    • Niraj Shah says:
      April 7, 2015 at 9:01 AM

      Check that your App ID and Secret are correct. If these are not correctly set then login won’t work.

      Reply
    • fazi says:
      October 4, 2016 at 1:24 PM

      Same problem here please any one let us know how to fixed it
      many of thanks

      Reply
  5. Marion says:
    April 6, 2015 at 8:28 PM

    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!

    Reply
    • Niraj Shah says:
      April 7, 2015 at 9:03 AM

      The FacebookCanvasLoginHelper should only be used on the first page that’s loaded in the canvas, as this code uses the signed_request to authenticate the user. You should store the signed_request to 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_request will be lost, so it’s your responsibility to make sure you save the session and load it when required.

      Reply
      • Marion says:
        April 8, 2015 at 11:25 AM

        Thanks for your answer. I understand, that it was a wrong way from me. I will do it with a own session.

    • Prashant says:
      April 7, 2015 at 5:11 PM

      Hey marion ,
      Did you proceed any further ?

      Reply
  6. ahmetkemalarikKemal says:
    May 6, 2015 at 2:59 PM

    I love you man.

    Reply
  7. mwsasser says:
    May 7, 2015 at 7:56 PM

    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?

    Reply
    • Niraj Shah says:
      May 8, 2015 at 3:14 PM

      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.

      Reply
      • mwsasser says:
        May 8, 2015 at 3:52 PM

        Would session_regenerate_id() affect it? That occurs where FacebookRedirectLoginHelper() is redirected.

      • Niraj Shah says:
        May 11, 2015 at 10:41 AM

        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.

      • mwsasser says:
        May 11, 2015 at 4:04 PM

        Apparently my problems were due to my aggressive htaccess. I’ve found a way around it, thanks for your help.

  8. Facebook API: Posting As A Page using Graph API v2.x and PHP SDK 4.0.x | Web Niraj says:
    May 11, 2015 at 4:11 PM

    […] 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 […]

    Reply
  9. Seth says:
    May 21, 2015 at 12:11 AM

    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.

    Reply
  10. Abhi Barua says:
    June 22, 2015 at 8:39 PM

    Hi, thanks for the nice tutorial, And I follow your website every time

    Reply
  11. roque potenza says:
    August 26, 2015 at 3:28 PM

    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?

    Reply
    • Niraj Shah says:
      September 4, 2015 at 7:14 PM

      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.

      Reply

Comment navigation

← Older Comments

Leave a Reply Cancel 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

PSN Profile

Tags

Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache API Application Backup Bug Command Line Cordova Example Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Flash Geolocation Google Nexus 5 Google Nexus One Hacking HTML5 Image Manipulation iOS iPhone JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2022 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