Site icon Web Niraj

Facebook API: PHP SDK Updated to v4.0.0

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:

See the gist on github.

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:

See the gist on github.

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:

See the gist on github.

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.

See the gist on github.


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:

See the gist on github.

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.

Exit mobile version