In this article I am going to show you how you can update your Facebook status using simple PHP code and Facebook Graph API. This is the most naïve way to use this, you can extend this to make your own script to update your status from mobile, via cron job, via email or whatever you think makes sense 🙂

Ok let’s get started now ! To be very frank, the status update script is already there in the SDK provided by Facebook, but very few people know. And there are people who won’t even know how easy it is.

First step is to create your APP in Facebook, let me show you how you can do that.

  1. Login to facebook and go to http://www.facebook.com/developers
  2. Click on the button at top right where it says “+ Set up  New App”image
  3. Now let’s give a name to your app.image
  4. Now next step is to put the information about the Facebook app which you are going to create. Not this wizard will not accept URLs like localhost; you have to specify a valid domain name where you are going to keep you app files.
  5. In the settings, you just need to put the following information which is mandatory at this stage. However, when you plan to make this application available for you all, you might want to input the complete details of your app.
    1. Under Web Site section on the left site, fill the following. You will notice the text is blurred for Application ID and Secret, as the name sounds you have to keep these safe with you and should not be disclosed publically. However, your application ID is public, but its safe not to disclose it in public.image
    2. Next is to put the Canvas Page and Canvas URL. This Canvas Page will be used to reach your application by the user. Keep it easy, yet unique. Canvas URL is the location from where Facebook pulls all the content of your APP. So be sure that you put you actual application files inside this folder. image
    3. And now we Hit “Save Changes” at the bottom of the page. If all settings are correct you will get a “Changes saved” message. You will see your application’s unique App ID, API Key, App secret , keep it safe and we are going to use these in our example.
  6. Now the application is setup, next step is to place the Facebook SDK in the same folder and update your status on Facebook – cool ?

Getting Facebook SDK and Update the status

You can download the latest version of SDK from https://github.com/facebook/php-sdk/ and hit download. The app information page also has a guide to make a clone of this git repo, but if you do not want to get yourself into all that. Simply hit download button from https://github.com/facebook/php-sdk/ and you should be good to go.

I have downloaded the latest version which is 2.1.2 till date. Extract the contents of this zip file and you should see three folders along with a readme file. We are going to use src folder which has the necessary code to interact with the Facebook API. Upload this folder to the http://yourdomain.com/fb-app-folder

Next step is to create your file which will update the status, let’s call it as index.php since it’s the default file which will be picked from the folder. You can download the file index.php and save it in the same folder. All code is commented for proper understanding.

A simple way to quick start is download the ZIP FILE of all the code and extract it to the folder which you specified in the step 5.2

You can refer to Facebook reference API for more functions and play around with the API.

 

Stay Digified !!
Sachin Khosla

Share this post: