facebook,howto,PHP,tutorials

Update your Facebook status using PHP and Graph API

30 Apr , 2011  

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 icon smile Update your Facebook status using PHP and Graph API

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 thumb8 Update your Facebook status using PHP and Graph API
  3. Now let’s give a name to your app.image thumb9 Update your Facebook status using PHP and Graph API
  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 thumb10 Update your Facebook status using PHP and Graph API
    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 thumb11 Update your Facebook status using PHP and Graph API
    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

, , ,


4 Responses to “Update your Facebook status using PHP and Graph API”

  1. Filipi says:

    Good morning Sachin Khosla, sorry to my english.

    I can post status in the wall, but when use cron not work… Already try everything and cannot to use cron for automatize. Can be anything permission? if not use cron work!

    My code:

    api_client->session_key = FB_SESSION;

    $fetch = array(‘friends’ =>
    array(‘pattern’ => ‘.*’,
    ‘query’ => “select uid2 from friend where uid1={xxx}”));

    echo $facebook->api_client->admin_setAppProperties(array(‘preload_fql’ => json_encode($fetch)));

    $sql = $base ->query(‘SELECT * FROM wall WHERE DATE_FORMAT( data_agendada, “%H:%i” ) = DATE_FORMAT(“‘ . date(‘Y-m-d H:i:s’) . ‘”, “%H:%i”) ‘);

    while ($result = $base ->tupla($sql)) {

    echo $result ['text_facebook'].”";

    echo “sign in”;

    if ($facebook->api_client->stream_publish($result ['text_faceboon].” Testing cron”))
    echo “Added on test FB Wall”;
    }
    } catch (Exception $e) {

    echo $e . “”;
    }
    ?>

  2. I have run many facebook apps via cron. No problem at all. Let me know what error do you get.

    Thanks
    Sachin

  3. Raaz says:

    Thats Awesome.. :) :)

  4. vir0e5 says:

    work for me… thanks sir !!

More in facebook, howto, PHP, tutorials (68 of 229 articles)


Your IP (Internet Protocol) is your computer's address and I am sure at times you want to know what is ...