Facebook API

  • 1.5.06
  • Comments(49)

Reason for the creation

It is startling to see that Facebook.com doesn’t yet offer an API, so I made my own. The API is as straightforward (see example) as it gets but I can just imagine some of the cool stuff that could be made using the information found in Facebook. (No longer the case)

Update (Aug 28, 2007)

Based on popular demand the unofficial Facebook API will be restored to its full glory. API 2.0 will be written in Java and will offer more flexibility and power than the old PHP one. If you would like to be notified of its progress or wish to help or wish to tell me anything else feel free to drop a line. Hopefully the development will be quick but who knows since school is about to begin in a week.

Update (Aug 2, 2007)

This API seems to still be sought after despite the public API having been released. Although I no longer offer the API publicly (although some googling will find it) it is still somewhat functional. Since Facebook seems to do everything in form of GET’s I have assembled a pretty good list of all the parameters for all the sites which would facilitate extending my API. However, since my API is not very “safe” I have restrained my use. If anyone is interested in the development or needs advice feel free to contact me. This is truly a fun pastime.

Update (August 16, 2006)

For a project that was conceived only half a year ago, I am surprised how successful it was. Success not because it accomplished all the tasks that a developer might have wanted, but because it brought some change into Facebook. The demand to liberate the information that was previously locked down inside the world of Facebook was the reason for this API to be built and now with Facebook’s own API, I consider this project to be complete.


  • Get profiles (and groups)
  • Poke friend
  • Search other students
  • Browse through friends
  • Messaging (partially implemented)


Returns true if login was succesful, else it returns false.

Returns array of the information in the student’s profile (based on the student’s id).

Returns array of friends of the student identified by the id. Filter selects which friends to retrieve. Possible values for filter are: ALL_FRIENDS, SCHOOL_FRIENDS, OTHER_FRIENDS, and UPDATED_FRIENDS. Pages represents the number of pages to be fetched.

Returns array of groups the user specified is part of (based on id). Pages represents the number of pages to be fetched.

FacebookAPI::advancedSearch(query[], pages)
Returns array with results of the query query. The query is a colletion of one or more of the following elements: name, email, sex(1=Male and 2=Female), city, state, zip, cell, phone, year (ex. 2005), room, and text. Pages defines how many pages of results to fetch from the query: the default is 1 page.

Returns true if poke is successful (based on student’s id).

Returns array of the currently logged in user

Returns currently logged in ID number.

Returns array of messages. Type specifies incoming and outgoing messages (use INCOMING_MSG and OUTGOING_MSG constants to specify which to fetch).

Input should be one of the entries returned by getMessage. It returns array with message and all threads the message may have.

FacebookAPI::sendMessage(id, subject, text[,msg])
Sends message to user with specified id. If the message is a reply to an earlier one, msg should be specified. Msg should be one of the entries from getMessages.


// Include API

// Creates new instance of the Facebook API
$foo = new FacebookAPI;

// Logins to Facebook

// Retrieves user’s profile in array form
$profile= $foo->myProfile();

// Example of how queries are returned:
echo $profile['Name'].' looks like this <img src="'.$profile['image'].'"/>';


  • Get id codes for fields such as status and schools (for searching)
  • Browse groups
  • Allow login with multiple accounts


  • Version 0.83 [txt]

Change Log:

  • 1/5/06: Initial release.
  • 1/9/06: Http header parsing fix to allow page redirection.
  • 2/23/06: Poke function added.
  • 3/7/06: Updated profile fetching routine.
  • 3/9/06: Fixed profile fetching routine. Took care of some bugs.
  • 3/12/06: Fixed image fetching RegEx.
  • 3/15/06: Fixed getFriends and advancedSearch functions.
  • 3/19/06: Basic messaging functions implemented.
  • 3/29/06: added getGroupMembership (thanks Adam W.)

Need help?

  • Email: andre (dot) cohen (at) gmail.com
  • AIM: musicbyte256


Use this API at your own risk. There should be no problem in the use of this API if kept for personal use; Otherwise you should probably get permition from Facebook.com

Jose Luna

  • 2.7.06

I haven’t made use of the API in any way, but this seems like it could offer some great functionality. I just wanted to note that you should keep up the good work, this is definitely a useful API.

Samuel Q. Slack

  • 2.11.06

Thanks for the API. I’ll certainly be using it.


  • 2.23.06

how about messaging / poking option?


  • 2.23.06

for some reason this happend when running facebookAPI.php :

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in ***/public_html/facebookapi.php on line 97

Andre Cohen

  • 2.23.06

Which version of PHP are you using? I believe the error comes from not using the latest version (5.1 I believe). If you remove the ‘private’ type from the beginning of all the funtions it should work (or download the new .6 version). Poke functionality has also been added to the API.


  • 3.1.06

can u help me , Im in stuck,I got these erros as follows when i execute the code
$foo = new FacebookAPI;
$profile= $foo->myProfile();
echo $page;
Notice: Undefined offset: 1 in C:\Program Files\Apache Group\Apache2\htdocs\Campus\facebookapi.php on line 303

Notice: Undefined offset: 1 in C:\Program Files\Apache Group\Apache2\htdocs\Campus\facebookapi.php on line 307

Notice: Undefined property: FacebookAPI::$myEmail in C:\Program Files\Apache Group\Apache2\htdocs\Campus\facebookapi.php on line 43

Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Program Files\Apache Group\Apache2\htdocs\Campus\facebookapi.php on line 280
Notice: Undefined offset: 1 in C:\Program Files\Apache Group\Apache2\htdocs\Campus\facebookapi.php on line 113

Notice: Undefined offset: 2 in C:\Program Files\Apache Group\Apache2\htdocs\Campus\facebookapi.php on line 117

Notice: Undefined offset: 1 in C:\Program Files\Apache Group\Apache2\htdocs\Campus\facebookapi.php on line 149

Andre Cohen

  • 3.1.06

Three things:
1)Notices are being shown because of the way your PHP version is setup. To remove Notices add the following line to the beginning of your script.
error_reporting (E_ERROR | E_WARNING | E_PARSE);

2)the getFriends function call is also not not what you want since you are trying to get a specific user. You should use the search function instead:

3) You run the risk of having found a bug. The fact that your e-mail has a subdomain may be causing problems resultsing in the fsockopen error. I shall look into that.


  • 3.2.06

Hi Andre Cohen,
The following code shows nothing but no errors
$arr = $foo->advancedSearch(array(‘email’=>”brb2w@alumni.virginia.edu”));
echo ‘Email:’.$arr["email"];

pls Help me


  • 3.2.06

advancedSearch returns an array of arrays. Therefor to get the first match you need to use $arr0['email'].


  • 3.2.06

Hi Andre

I also try this and also try print_r($arr) But it outputs as follows:
Array ( [0] => Array ( [] => ) )

Andre Cohen

  • 3.5.06

I tried the same scenario that you have presented and have not found any indications for error. Are you sure you are login is successful? Your error may be related to the fsockopen() error you had previously (most likely due to the way PHP is configured or firewall)


  • 3.7.06

thanks for checking it out.. umm.. now i am not getting any error messages, there is simply no information in the variable…

using your test example, it outputs
“looks like [broken image]“

maybe i am using the API file wrong, do i simply need a line of code such as:



Thanks again for your help.

Andre Cohen

  • 3.7.06

Ah.. thank you so much for pointing this out: they changed the site’s format. It has been fixed so you should download the new API and everything from the example should work.


  • 3.8.06

feeling foolish.. downloaded the new API, still getting the “looks like this [broken image]“

i am simply using the example in a file entitled facelift.php with
– i’ve saved the contents of 0.7 to the facebookapi.php file.

Not sure why I’m having so much difficulty.
I appreciate your help!


  • 3.8.06

If you are using the example, what do you get if you do print_r($profile)? The only way I can imagine for you to be having problems is if loging in is not working.


  • 3.8.06

Array ( [] => )

Again, is the appropriate way to use this simply with a require or include statment?


  • 3.8.06

Andre, I appologize for this.. I checked my version and it looks like the server only has 4.4.2 on it. I’m curious as to what functionality the script has that is native to 5.1.2 — do you know of any workarounds? Such as function definition lists which will let 4.4.2 run as 5.1.2? Any suggestions on getting this to work on the older build would be helpful. THANKS!


  • 3.8.06

Looking at the code, there is nothing that should not work in version 4. The issue actually lies in the change that Facebook made in the profile page. The parsing at the moment is broken and requires fixing. I’ll get to that at soon as I can (sooner if it were not for midterms).


  • 3.9.06

cool. i’ll see if i can fix the parsing in my local copy untill we’re blessed with the next release..


  • 3.9.06

Alright, it works (that’s until they change the format again). There were also minor problems fixed that clearly made previous versions impossible to function.


  • 3.12.06

looks like your profile array keys are title case, but in your example they aren’t.. also, can’t seem to find the ‘image’ key in the array. I am getting everything else back inside the variable though.. just not the image location…


  • 3.15.06

Great API, im loving it! ONe quick question…why when i use the GetFriends function for myself I get all my friends but for other people only 50?


  • 3.15.06

Andre — amazing as always… It might be useful to parse the friends ID from the POKE THEM link, as opposed to their profile link, as this will return the user ID for friends not from the same school. And still can’t wait to have message / wall writing functionality from within the API.

I’m working on a ‘text message’ all friends with a third party text messaging service API…


  • 3.15.06

Ben: Partially it was the way you were using the function, and it was partially a bug in the API as well (which has been fixed).

To get the other friends that a friend has you must use the $page argument which specifies the number of pages to fetch. Note that this could take some time (depending on the number of pages being fetched) and consequently the script will abort.


  • 3.22.06

looks like the .txt file is still .75
looking forward to playing with .80
thanks again Andre!

Andre Cohen

  • 3.22.06

my bad, I forgot to upload the file. It’s up now.

John Boxall

  • 4.5.06

Andre / all i find if you change the last line of your example to the following:

echo $profile['Name'].’ looks like this ‘;

It works much better!

Thanks for this kickin’ api…

John Boxall

  • 4.5.06

Try this one more time – the last line should look like this:

echo $profile['Name'].’ looks like this ‘;

John Boxall

  • 4.5.06

Comments strips the img tag :| – replace the ‘|’s with ” …

echo $profile['Name'].’ looks like this |img src=”‘.$profile['image'].’” /|’;


  • 4.5.06

Thanks for pointing it out.


  • 4.20.06

I love your first sentence.

Adam Doupe

  • 4.26.06

Hey just fyi, Facebook changed the search procedure. (Damn them). To fix it, and at least be able to login (havn’t tested the other functions) you have to make some changes.

$SearchURL should be “facebook.com/s.php”

in AdvancedSearch, do the following changes:
Set $str = “”;
Then change the last line to:
return $this->parseBrief($page,”result clearfix person”);

Also, they only take in a q parameter (no advanced search AFAIK. So change the second to last line of the login function to:

Then, you must use this same syntax for using the advanced search.

Hope this helps, I’ll be posting changes/quickfixes that I make to get this working.

- Adam Doupe

Adam Doupe

  • 4.28.06

Hey I’d watch out using this script. I got my account disabled and they told me:

“Our systems indicate that you’ve been highly active on Facebook lately and viewing pages at a high enough rate that we suspect you are running an automated script. Activity of this kind is a violation of our terms of service and potentially of federal and state laws. “

Just a word to the wise, be carful how many pages you crawl and how often.


  • 5.10.06

Adam — did you get your account back?

Federal & State Laws? What the hell are they talking about….


  • 6.11.06

Where is the link to download the file, facebookapi.php? Maybe I am blind but I could not find it above.


  • 6.22.06

Yes, I cannot find the download link either.


  • 7.6.06

Where can I download your API? It does not seem to be linked on this page.


  • 7.6.06

ditto. would love to see it.


  • 7.21.06

I added on to an existing FB API implemented in Perl a while back. The original script got shushed (as I’m guessing yours did).

I eventually got all the features you listed working (except searching), along with mass-walling. I also buffed it to crawl through profiles at my school and send out friend requests.

After a while, my account was disabled for 10 days, not because they detected “script-like activity”, but because too many people had declined my friend requests. They have all sorts of rate-checks to help prevent the use of scripts.

That said… I have been talking to someone… and I wouldn’t be surprised if they released some kind of developer API in the next month or two…



  • 11.16.06

Where’s the API? I can’t DL it!


  • 11.17.06

doea anyone know a site that i can log onto facebook with out going there because skool blocked all sites that have facebook in the title

Shahid Mehmood

  • 6.4.07

Hi Andre,

I have visited your facebookAPIÂís and that are greate. I really liked them very much.

Can you please help me a little. As you have worked on FaceBook a lot.

Is there anyway that we can count the total number of users on a FaceBook?

If no then is there any other way to accomplish this.

Your help will be greatly appreciated.

Waiting for your response.


  • 6.4.07

No, I don’t believe there is any way to knowing exactly how many users are on facebook.

One method for estimating would be to count the number of members in each network (something that could easily be done once you have a list of all the network). I don’t believe, however that their current API allows access to their network pages. So this might not be so easy.

Shahid Mehmood

  • 6.4.07


Shahid Mehmood

  • 6.5.07

Hi Andre,
Thanx for your help.
I need a little more help from you.

Is there any way that we can collect the information of the user when he/she visits someone else profile which has our application in his/her profile section?


  • 6.5.07

I believe that that sort data collection might infringe on the terms and conditions. However ignoring that I don’t believe there is any direct way to do that except if found by working around the system.

I believe that the most promising method (when using their Application API) is if the user has a link to your application present (on the Nav bar). Since the link and icon is probably lives in your own server you could easily get the information about who is loading the icon/link and on what page they are on.

I know that they work hard on maintaining their users privacy so I doubt this method or any other will work.

Shahid Mehmood

  • 6.7.07

Hi Andre,
Thanx again for your answer.
I have a little bit more trouble if you can guide me I will be very thankful to you.

Suppose someone has added our application which is in his profile, when other user visits his profile can we get the querystring value of the user whose profile is being visited?

Waiting for ur response.


  • 6.18.07

I’m trying to get a copy of the API but the wayback link doesn’t work and nor does the download link on this page.

Where can I get it?


Leave a comment