Jaslabs: High performance Software

High Performance Software

How to use the facebook API

By Justin Silverton

The facebook API allows you to access profiles, friends, photos, and events.  To access the API from an outside server, you need to go through the following steps:

Getting started

Place the following in a file called “client.php”.  Also, facebook_conf.php and facebookapi_php5_restlib.php need to both be in the same directory.

include_once ‘facebook_conf.php’;
include_once ‘facebookapi_php5_restlib.php’;

$rest_server = ‘api.facebook.com/restserver.php’;
$api_key = (Your api key from facebook);
$secret_key = (your secret key from facebook);
$session_key = md5(time());

Note: This is just a sample session key.  This should be a unique key that can identify the current session user.  It can also be placed in a cookie.

$client = new FacebookRestClient($rest_server, $api_key,$secret_key, $session_key, false);
$session_info = $client->auth_getSession($auth_token);
$client->session_key = $session_info[’session_key’];
$uid = $session_info[’uid’];
$session_info = $client->auth_getSession($auth_token);

//Here are some sample pieces of information that can be retrieved
$your_messages = $client->messages_getCount(); //get your total number of messages
$your_wall_count = $client->wall_getCount(); //get the number of posts on your wall
$p_albums = $client->photos_getAlbums($uid); //get list of photo albums

The facebook api can do much more than explained in these examples.  For more information, go here: http://developers.facebook.com/documentation.php.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati

20 Comments so far

  1. Stuart December 25th, 2006 11:12 pm

    I have been trying this over and over and I keep getting this error

    Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /home/httpd/vhosts/#######.com/httpdocs/facebookapi_php5_restlib.php on line 40

    This is the default facebookapi_php5_restlib.php that I downloaded from facebook. Why? Any help is appreciated.

  2. Justin Silverton December 25th, 2006 11:27 pm

    Are you running PHP 5?

    create a small php script with the following in it to double check:

    < ?php phpinfo(); ?>

  3. Stuart December 26th, 2006 12:23 am

    Nope it looks like Im running PHP Version 4.3.10

    Will it not work with this? Is there a PHP 4 library?

  4. Stuart December 26th, 2006 1:04 am

    I found the unsupported PHP4 lib file on the same page. Sadly the sample file only pulls in the User’s name so I will need to try and learn how to make all the other info display.

  5. Tyler Lee February 9th, 2007 6:35 pm

    I am new to PHP…i have done the items above but where do i put the “Getting started” code…I am using DreamWeaver but i am new with that also..I can figure out anything just need a little direction

  6. Justin Silverton February 9th, 2007 8:07 pm

    Tyler,

    Do you have a server where you can run your php scripts (you are going to need a webserver+php to run any of the facebook API scripts)? If not, you can install apache+mysql+php easily on your windows machine using wamp (link here: http://www.wampserver.com/en/).

  7. TDempski May 23rd, 2007 11:33 am

    a partner and I have been working on getting a system together for are site. Well, I was wondering, I not very great at writing PHP, but I have built sites using PHP with Dreamweaver. I was wondering is there a way to create dynamic tables and such with the facebook API on Dreamweaver?

  8. Justin Silverton May 23rd, 2007 10:39 pm

    TDempski,

    are you trying to access the facebook api without using PHP?

  9. TDempski May 24th, 2007 12:32 am

    No, I plan on using PHP 5, I don’t have a strong coding background, just a design background… I am looking for simple way of getting Dreamweaver to do all the hard coding.

  10. TDempski May 24th, 2007 12:41 am

    I guess I should mention that I do know how to setup a connection to a database and make calls to the database on Dreamweaver. What I wanted was to do the same thing.

  11. Justin Silverton May 26th, 2007 11:36 pm

    You may want to start looking at the new additions that have been made to the facebook API:

    http://developers.facebook.com/documentation.php?doc=fbml

    I don’t think you can actually do all the hard coding with dreamweaver. You need to use one of the PHP/java/flash libraries (or make the calls yourself).

    You probably have see this already, but here is a direct link to the php5 library:

    http://www.f8.facebook.com/fbml/facebook-platform.tar.gz

  12. Shaolin July 8th, 2007 4:53 pm

    Hi Justin,

    I did what you requested and ran the script on my server but I see a blank page ? What exactly is this meant to do ? Im quite new to php, so forgive my ignorance.

    Thanks

  13. Adnan Siddiqi July 15th, 2007 3:41 am

    is it necessary that facebook apps HAVE to be run within FACEBOOK platform? what I mean that can I access certain infor from facebook server and display on my own Website?

    Thanks

  14. links for 2007-08-08 « Simon McManus August 8th, 2007 8:28 am

    […] » How to use the facebook API? - Jaslabs: High performance Software facebook get friends php code (tags: facebook php) […]

  15. william October 9th, 2007 10:31 pm

    look guys its great that you all want to take the initiative to create a facebook application. But if you are expecting dreamweaver to do it all for you, you are in for a stark realization. THERE IS NO EASY WAY. You have to learn php, sql, javascript, ajax, xml, html, lamp, all that stuff. I have been doing this for the past few years and I still find some things difficult. But I will tell you the only way to know is to get your hands dirty and CODE! make mistakes, it takes on average 1 bug for every 3 lines of code. You are going to be frustrated but just DO IT!

  16. oyunlar November 14th, 2007 12:25 pm

    Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /home/httpd/vhosts/#######.com/httpdocs/facebookapi_php5_restlib.php on line 40

    =(

  17. Justin Silverton November 14th, 2007 1:46 pm

    “Parse error”

    have you downloaded the latest php5 library?

  18. Abc January 2nd, 2008 4:28 am

    Hi,

    I have a created one application in PHP. From this application I want to post a Wall and Message of the facebook friend. So please help me. What should I do for posting a Wall and Message of the Facebook friend?

    Thanx

  19. John January 11th, 2008 2:51 pm

    Where is the var $auth_token being instantiated? I think the reason why its not working is because $auth_token is null.

  20. Frank January 25th, 2008 3:52 pm

    I cannot find anywhere a file named “facebook_conf.php” to download. Do you mean “facebook.php” ?

    How many lines of code does “client.php” contain? (6 or 12?)

    Is this client,php supposed to be included in any file? (like index.php?)

    Ths is the only article I can find that mentions anything about “client.php” - and I cannot get the API’s to work, so I want to try your approach.

    Thank you

Leave a reply