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):
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.
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.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() ?
From my understanding of the Facebook SDK, the session will always have
FBRLH_state
. You are better looking forcode
in the URL, and if it’s not present then callgetLoginUrl()
.[…] 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 + […]
How i enable photo uploading?
You need to ask for the
publish_actions
permission to allow posting of photos to the timeline.This is still working?? (6/9/15)
Thanks!!! 😀
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.
What’s the code for logout?
The code for the
logout.php
page can contain something like: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
Unfortunately I have no idea what could be causing this. Does the hyperlink you are using include an
access_token
orcode
parameters, and could that be conflicting with the Facebook SDK?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!
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.
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
Eureka. It was my $loginUrl. I did not notice I did not at www.
how to make an app to post on friends wall
Posting to Friends’ wall is no longer allowed. You can only tag friends in posts on your own wall using the
taggable_friends
API.Hi, i am wondering will it work with the call of ‘me/feed’?
I tried but mostly it gives an empty array.
Are you trying to publish a post or retrieve the user’s feed?