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: Using the Facebook PHP SDK v4.1.x

Facebook API: Using the Facebook PHP SDK v4.1.x

21

As version v4.1 of the Facebook PHP SDK nears release, I though this would be a good time to explain how the new version works. This is the first major update to the SDK since it was released in May 2014, and one that has been mostly lead by the open-source community.

It’s worth noting that v4.1 of the SDK is quite different to v4.0.x, and anyone upgrading to the new version should be aware that it will break your application until you update your integration. Unfortunately, there is no easy way to migrate to the new version.

Warning: Migrating from v4.0.x to v4.1 will break your application.

Getting Started

Assuming you’re not using composer, here is how you include the SDK files in your code:

If you’re using composer, you’ll only need to include the session_start() part on line 10 (again, assuming you haven’t started the session elsewhere).

Note: At time of writing, I’m waiting for a pull request to be merged into the SDK to fix an issue with the autoload script. If this has not been merged, you will see a Fatal Error in the next step. Please see the diff of the pull request to see how to fix the issue.

Setting up your Application

You can then initialize the SDK using your API Key and API Secret from Facebook. In addition, you can (optionally) set the default HTTP Client (curl) and Persistent Data (PHP sessions) handlers.

Login and Make API Call

The next step is to log the user into your application and make a simple GET API call. This is a bulk of the code, as we need to check if the user has already been authenticated and then make the API call.

Detailed Code Explanation

Complete Code

POST Example

You can use the post() method to make POST call to the API. For example, the below code demonstrates how to publish to the user’s timeline (provided that the publish_actions permission has been granted):

Facebook, Facebook Graph API, Facebook PHP SDK 4.1, PHP, Tutorial

21 comments on “Facebook API: Using the Facebook PHP SDK v4.1.x”

  1. James says:
    March 4, 2015 at 12:24 AM

    I just tried using this script after upgrading to PHP 5.5.x and get: Fatal error: Class ‘Facebook\Facebook’ not found in … index.php on line 13
    Your change to the autoload.php script is implemented. I don’t even know where to start looking… I just started learning php.

    Reply
    • Niraj Shah says:
      March 4, 2015 at 5:54 PM

      The error is likely to do with the location of the Facebook SDK files. In my Facebook/ folder, I only have the PHP files from the SDK. If you downloaded the zip from github, copy the /facebook-php-sdk-v4/src/Facebook folder to your code directory and my tutorial should work.

      Reply
  2. Niko Jojo says:
    March 10, 2015 at 11:58 AM

    Regarding to your answer for url : https://stackoverflow.com/questions/25559796/facebook-isvalidredirect-always-return-false/25565686#25565686

    When facebook returns to my url, its calling again getLoginUrl() function.

    So my question is, is it valid to give condition like if($_SESSION[‘FBRLH_state’] == ”)
    then only call function getLoginUrl() ?

    Reply
    • Niraj Shah says:
      March 10, 2015 at 12:05 PM

      From my understanding of the Facebook SDK, the session will always have FBRLH_state. You are better looking for code in the URL, and if it’s not present then call getLoginUrl().

      Reply
  3. Facebook: Are Your Applications Ready for Graph API v2.x | NUETA VENTURES says:
    March 16, 2015 at 2:49 PM

    […] update your applications to use the newer Facebook PHP SDK. We have a guide to help you upgrade to upcoming Facebook PHP SDK v4.1. Need help? We can provide help and advise, or do the upgrade for you from £40 / hour + […]

    Reply
  4. ajithc1861jith says:
    March 31, 2015 at 2:04 PM

    How i enable photo uploading?

    Reply
    • Niraj Shah says:
      April 7, 2015 at 8:52 AM

      You need to ask for the publish_actions permission to allow posting of photos to the timeline.

      Reply
  5. Renato Slash says:
    June 9, 2015 at 6:21 PM

    This is still working?? (6/9/15)
    Thanks!!! 😀

    Reply
    • Niraj Shah says:
      June 22, 2015 at 9:23 AM

      Yes, this still works, although Facebook PHP SDK 4.1 never released. The next version of the SDK is likely to be v5.0, which will again have more changes.

      Reply
  6. Drew says:
    June 24, 2015 at 4:57 AM

    What’s the code for logout?

    Reply
    • Niraj Shah says:
      July 10, 2015 at 5:04 PM

      The code for the logout.php page can contain something like:

      // destroy the session
      session_destroy();
      
      // redirect back to website home
      header( 'Location:' . $site_url );
      

      Reply
  7. Christiaan says:
    August 14, 2015 at 2:46 PM

    I have a problem on the landingpage after login. When making the first request to the API I receive the error “Graph returned an error: Error validating access token: Session does not match current stored session. This may be because the user changed the password since the time the session was created or Facebook has changed the session for security reasons.. The weird things is that this does not happen when I navigate to the page via typing the url in the address bar. Only when I access the site via a hyperlink

    Reply
    • Niraj Shah says:
      August 14, 2015 at 8:11 PM

      Unfortunately I have no idea what could be causing this. Does the hyperlink you are using include an access_token or code parameters, and could that be conflicting with the Facebook SDK?

      Reply
      • Christiaan says:
        August 14, 2015 at 8:18 PM

        He Niraj, Thank you for your quick reply. No it does not. Even when I go the the page containing the login button it does not work. For example you can check http://www.youddress.com/foundation (login button at top right) . when you access it via a link you will see the error i mentioned above, although after typing the url in the address bar, the login works fine. I don’t see why there would be a difference between these 2 options. thanks again!

      • Niraj Shah says:
        August 14, 2015 at 8:24 PM

        Looks like there is an error in the way you log the user into your website. After logging in and seeing the error, I went to the link again and I was logged in correctly. Double check your login code and make sure you’re not making any incorrect API calls.

  8. Christiaan says:
    August 17, 2015 at 9:48 AM

    He Niraj. I double checked a lot of things but somehow I can’t find a fix for this. It’s really bugging me as obviously most people will visit the site through a link. Thank you for your help. I hope I find the problem somehow

    Reply
    • Christiaan says:
      August 17, 2015 at 1:48 PM

      Eureka. It was my $loginUrl. I did not notice I did not at www.

      Reply
  9. suhin says:
    October 18, 2015 at 2:46 PM

    how to make an app to post on friends wall

    Reply
    • Niraj Shah says:
      November 3, 2015 at 9:47 AM

      Posting to Friends’ wall is no longer allowed. You can only tag friends in posts on your own wall using the taggable_friends API.

      Reply
  10. geetikajoshi07etika says:
    December 21, 2015 at 9:28 AM

    Hi, i am wondering will it work with the call of ‘me/feed’?
    I tried but mostly it gives an empty array.

    Reply
    • Niraj Shah says:
      January 18, 2016 at 3:46 PM

      Are you trying to publish a post or retrieve the user’s feed?

      Reply

Leave a Reply to Facebook: Are Your Applications Ready for Graph API v2.x | NUETA VENTURESCancel 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 Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Firewall Flash Gadget Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio PS4 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