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:
- login to your Facebook Account
- get an account key: http://developers.facebook.com/account.php?action=add_key
- Get the client library: http://developers.facebook.com/documentation.php?doc=clients (I will be discussing the PHP API, but it is also available for c#, c++, java, perl, python, ruby, and visual basic.NET).
- The php 5 library can be found here: http://developers.facebook.com/clientlibs/php5_client.tar.gz (curl is required)
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.
20 Comments so far
Leave a reply






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.
Are you running PHP 5?
create a small php script with the following in it to double check:
< ?php phpinfo(); ?>
Nope it looks like Im running PHP Version 4.3.10
Will it not work with this? Is there a PHP 4 library?
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.
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
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/).
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?
TDempski,
are you trying to access the facebook api without using PHP?
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.
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.
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
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
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
[…] » How to use the facebook API? - Jaslabs: High performance Software facebook get friends php code (tags: facebook php) […]
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!
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
=(
“Parse error”
have you downloaded the latest php5 library?
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
Where is the var $auth_token being instantiated? I think the reason why its not working is because $auth_token is null.
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