Will Perone

Developing a Facebook App in PHP4

Getting started developing a facebook app in PHP4 is a bit tricky; here's how to do it. First off, you're going to need to know what version of PHP your webhosting service is using. If you don't already know this, you can write a simple page like the following and put it on your site.
<?php  echo "PHP version " . phpversion(); ?>
Now you should have a definite answer on what version of PHP you have. Next you may have noticed when looking through the facebook step by step guide that you will just get a blank screen when you run their code. That is because PHP4 does not support try catch exception logic. From studying the facebookapi_php4_restlib.php file though you can see that all of the functions are called through this call_method function. The call_method function in the PHP4 client is different than the 5 client in that instead of throwing an exception, it just sets $this->error_code = $result['error_code'];. That is what we need to check instead of having the exception handler. This means that we can convert what they had in PHP5:
//catch the exception that gets thrown if the cookie has an invalid session_key in it
try {
  if (!$facebook->api_client->users_isAppAdded()) {
} catch (Exception $ex) {
  //this will clear cookies for your application and redirect them to a login prompt
  $facebook->set_user(null, null);
To PHP4 like the following:
$isAppAdded= !$facebook->api_client->users_isAppAdded();
if ($facebook->api_client->error_code!=0 && $isAppAdded) {

if ($facebook->api_client->error_code!=0) {
  // this will clear cookies for your application and 
  // redirect them to a login prompt
  $facebook->set_user(null, null);
Even after this point you may notice however that the example still doesn't work. Even better, you may also be getting an HTTP error 405. Their example has another bug where they are using the wrong callback url; the $callbackurl should be set to your facebook app url (http://facebook.com/yourapp/). Here is a simpler complete example than the one that's provided in the tutorial. Make sure facebook.php and facebookapi_php4_restlib.php are in the same directory as your index.php (the following code):
if (isset($_REQUEST['mockfbmltext'])) {
  echo $_REQUEST['mockfbmltext'];

require_once 'facebook.php';

$appapikey = 'your api key from when you set up your app in Facebook';
$appsecret = 'your api secret from when you set up your app in Facebook';
$facebook = new Facebook($appapikey, $appsecret);
$user = $facebook->require_login();

$appcallbackurl = 'http://apps.facebook.com/yourapp/';

//catch the exception that gets thrown if the cookie has an invalid session_key in it
$isAppAdded= !$facebook->api_client->users_isAppAdded();
if ($facebook->api_client->error_code!=0 && $isAppAdded) {

if ($facebook->api_client->error_code!=0) {
  // this will clear cookies for your application and 
  // redirect them to a login prompt
  $facebook->set_user(null, null);

echo "<p>hello $user</p>";

if (isset($_REQUEST['profiletext'])) {
  $facebook->api_client->profile_setFBML($_REQUEST['profiletext'], $user);
  $facebook->redirect($facebook->get_facebook_url() . '/profile.php');

echo '<form action="" method="get">';
echo '<input name="profiletext" type="text" size="30" value=""><br>';
echo '<input name="submit" type="submit" value="Display text on profile">';
echo '</form>';

$fbml = <<<EndHereDoc
<input name="mockfbmltext" type="text" size="30">
<br />
<input type="submit"
  clickrewriteid="preview" value="Draw text below"
<br />
<div id="preview" style="border-style: solid; border-color: black;
  border-width: 1px; padding: 5px;">

$facebook->api_client->profile_setFBML($fbml, $user);

echo "<p>the following form was added to the profile box:</p>";

echo $fbml;
If you are getting HTTP error 405, that means that your webhost isn't understanding your callback url from facebook. In the Facebook Developer page, go to "See My Apps" (on the right side) and click Edit Settings. You will have to specify your complete URL including the index.php in the callback URL box.

Also, after you set "Can your application be added to Facebook" to yes, you will also need to check the radiobox "Who can add your application to their Facebook account?" for the setting to save right. And on a final note, facebook applications can only have letters, hyphens and underscores in the canvas page URL and if you type in an invalid or taken one, all of your fields will be erased and you will have to start over so make sure you type in a correct Canvas Page URL the first time!

Now you should have a fully functional Facebook App in PHP4!

Praveen 5 19
Thanks a ton ..
Just one step from your blog & my FB app was up & running.
sanjeev 0 0
Hi sanjeev here i got this problem please solve it :

PHP version 4.1.2
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in c:\wamp\www\popflyz\onlinechating\facebookapi_php4_restlib.php on line 274

Fatal error: Cannot instantiate non-existent class: facebookrestclient in c:\wamp\www\popflyz\onlinechating\facebook.php on line 51
Will 1 0
That seems odd; line 51 of that file is just part of a comment in the original source although the next line is the constructor:
function FacebookRestClient($api_key, $secret, &$facebook, $session_key=null);

You may also want to take a look at this guide: http://phpjoint.com/index.php?option=com_content&task=view&id=80&Itemid=37
although that file should already be PHP 4 compliant. Hard to say without seeing the source.
Your Name 2 0
1 0
blake 1 0
Fatal error: Call to undefined method Facebook::require_add() in /home/memphisb/public_html/facebook/index.php on line 13

error - any thoughts?
Reko 1 0
Thanks a lot!! Now it work fine :)
Ore 0 0
Hey I recked my brain for hours on end trying to fiqure out with this wasn't working. My server does run php4, thanks for the code. How about the invite friends code. Nothing seems to work.
revan 1 0
thank u very much
francois 1 0
to blake

require_add is deprecated, use require_login instead
don 2 0
Thank you very much! -donovan tierra
Kuiyatanchi 2010/12/13 Contact Me2 0
hi i am new here. I just wrote to say i want to learn it.
Matee 2011/07/09 Contact Me1 0
Now I feel stupid. That's cerlead it up for me
ArridaVix 2013/12/15 Contact Me1 0
Actos No Prescription Day Summary Diabetes Mellitus Ambulatory . How Long Does It Take For Macrobid To Start Working Topamax And Bladder <a href=https://archive.org/details/Oxycodone5Mg30Mg>can you buy oxycodone legally</a> Depakote And Dehydration Middle Blood Pressure Through Exercise Orlistat Diet Pill Results Does Blood Pressure Practice Cause Weight Gain . Buy Famvir Canada Prometrium For Hot Flashes Patient 20 Weight Loss Diets <a href=https://archive.org/details/BuyAmoxicillinAugmentinOnlineNoRx>Amoxicillin No Prescription Overnight Delivery</a>. Enalapril Drug Angiotensin Converting Enzyme Augmentin Sinus Infection Clavulanate Potassium Lorcet Watson 503 Sterapred Side Effects .
Oppona 2015/08/18 Contact Me0 0
XRumerTest 2016/09/18 Contact Me0 0
Hello. And Bye.

<- for private contact