#native_company# #native_desc#
#native_cta#

PHP Development on the Facebook Platform: Building Your First App Page 2

By Sachin Khosla
on April 19, 2010

Now, It’s Your Baby!

Now that you have set up the application, let’s have a look-see at how to put some muscle on your bare-bones application. In this example, we will use Facebook’s PHP client library, which is a compressed file. After downloading the client library, extract the files. You will get two folders namely, footprints and php.
Footprints is a sample application provided to help you understand how the Facebook API works. However, because the API functions keep changing, this code might have some deprecated functions. The php folder contains the library files that you will use to talk back to the Facebook API.
Let’s start with the code by including the library file called facebook.php, as shown in Figure 1.

Facebook Library  Folder Structure
Figure 1. Locate facebook.php in Facebook Library Folder

Create a file called index.php and copy the following code into it:

<?php

require_once 'php/facebook.php';

$appapikey = 'INPUT YOUR APP KEY HERE';
$appsecret = 'INPUT YOUR APP SECRET KEY HERE';

//create the object
$facebook = new Facebook($appapikey, $appsecret);

/**
 * Check if we have an already logged in user ?
 * Yes - Skip the require_login
 * No - User is redirected to facebook login page
 */
if(!is_numeric($facebook->get_loggedin_user()) || !$facebook->api_client->users_hasAppPermission("publish_stream"))
  $facebook->require_login($required_permissions = 'publish_stream');

/**
 * In case user reaches here and our application doesn't have proper permissions to publish,
 * then display this error message
 */

if(!$facebook->api_client->users_hasAppPermission("publish_stream"))
{
  echo 'Darn !! Something just broke !!' ;
}

/**
 * Check if we have Post data and a status to update
 */
if($_POST['update_me'] == 1)
{


  $res=$facebook->api_client->stream_publish($_POST['st'],'');
  $uid = $facebook->get_loggedin_user();

  $tmp = explode ("_",$res);
  if($uid == $tmp[0])
  {
    echo "Timeline updated successfully, <a href='http://www.facebook.com/?id=$uid'> Go back to your profile page</a>";
  }
  else
  {
    echo "Couldn't update your status, Please try again";
  }
}
/**
 * Display the form to update status
 */
else
{
  ?>

<form method="POST" action="http://www.your_domain.com/index.php" >
  <input type="hidden" value="1" name="update_me"/>
  <textarea name="st"></textarea> <br>
  <input type="submit" value="Update status" />

</form>
  <?php
}
?>

Important Tasks to Remember

Make sure you complete the following tasks to properly build your application:
  1. Include the library file (facebook.php) and change the path in require_once to match your folder hierarchy.
  2. Copy/paste the API key and secret key before trying to use your application. It won’t work without these anyhow.
  3. Input the canvas page URL right to the path of the script on your server/domain.
When you execute the above script, it will look for logged in users, if any. If a user is not logged in, then it redirects the user to the Facebook login page. This is also handled by the require_login function, but the problem with require_login is that it redirects so often that any post data to your page is lost. So, you have put a check before calling the require_login function. In the same line, you also check whether or not your application has permission to post updates onto a user’s stream. If you do not have permission, the user is prompted to allow your application.
When you have permissions and post data, the status is updated to the user. When a status is successfully posted, a response of the type userid_postid is returned. If it fails, then different error codes are returned. Find a list of error codes on Facebook’s wiki page.

What Have You Learned?

You have just learned how to start building your own Facebook applications. You can extend the above code to suit your requirements. If you feel confident, you can move on to write some advanced programs using FBJS and XFBML. Other than that, you should keep abreast of any functions that Facebook deprecates — particularly those that your code might be using. Otherwise, your application might crash all of a sudden.
To view a working demo of the app described in this example, see Facebook’s Status Updater.
Sachin Khosla is a Web developer and technology evangelist who has written and spoken extensively about open source technology. Sachin is part of an active open source community that organizes OSScamp in Delhi, India. To learn more about this author, read Sachin’s blog at Digimantra.com.

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:

Sachin Khosla

Comment:



Comment: