PHP

From Facebook Developer Wiki

Jump to: navigation, search

Contents

Official PHP Client Library

The official Facebook PHP Client Library package contains three directories and a README file.

  • The php directory contains these core components:
    • facebookapi_php5_restlib.php contains the implementation of the methods that constitute the client library.
    • facebook.php contains a class that can be used by the web client application.
    • facebook_desktop.php contains a class that can be used by the desktop client application.
  • The php4client directory contains similar components for PHP4.
  • The footprints directory contains code for a sample application called "footprints".
  • See the facebook-platform/footprints/README file (from the .tar.gz archive) for additional information.
    • one way of displaying the currently installed PHP libraries is to create a file named info.php containing the following code (delete file after use):
<?php phpinfo(); ?>
  • when editing the facebook-platform/footprints/config.php file, note the following:
  • when editing the facebook-platform/footprints/index.php file, note the following:
    • for greater security, the config.php file should be placed in a directory that is not accessible by end-users - if your hosting provider places your files into your home directory, the following steps can be followed:
      • create a directory called private_footprints in your home directory
      • move config.php into this directory
      • change the config.php include statement in index.php as follows:
// this defines some of your basic setup include_once '/home/username/private_footprints/config.php';
  • any references to the "framed page URL" in the README and config.php files are analogous to referring to the Canvas URL
    • to replace all references to the default footprints application Canvas URL in the lib.php file, try running the following from a bash shell:
sed -i -e 's/\/footprints/\/yourappname/g' facebook-platform/footprints/lib.php grep apps !$


Facebook Sample Application

Facebook created a sample application called Smiley that demonstrates all the major features of the new profile. You can download the Smiley source code. Note that Smiley & Facebook PHP Client Libraries are designed to work on PHP5.

Unofficial PHP5 PEAR package Services_Facebook

The current PHP5 library from Facebook doesn't support all of the API endpoints, photo uploads, etc. Services_Facebook supports all of the endpoints, except Data (partial experimental support is in SVN), along with photo uploads and it ships with unit tests that cover 100% of the code. Download the code from PEAR or visit the Google Project.

Usage

You can review a more detailed version of this code on Talk:PHP.

<!-- App needs to be set as FBML in application settings for this to work --> <!-- For simplicity, this requires the user to add the application before continuing --> <!-- See: http://wiki.developers.facebook.com/index.php/Fb:if-is-app-user --> <!-- If the user is an app user, then do everything --> <!-- Otherwise, skip to the end for similar FBML redirecting to add application page --> <fb:if-is-app-user> <?php // One of the main usages of PEAR is so the following include will work unaltered require_once 'Services/Facebook.php'; // Set globals Services_Facebook::$apiKey = 'xxx'; // insert your application key instead of xxx Services_Facebook::$secret = 'yyy'; // insert your application secret instead of yyy try { $api = new Services_Facebook(); // If a session key is passed, then grab it if (!empty($_POST)) { if (!empty($_POST["fb_sig_session_key"])) { $api->sessionKey = $_POST["fb_sig_session_key"]; } } if (!empty($_POST)) { if (!empty($_POST["fb_sig_canvas_user"])) { $isCanvasUser = true; $uid = $_POST["fb_sig_canvas_user"]; } else { $isCanvasUser = false; } if (!empty($_POST["fb_sig_user"])) { $isAddedUser = true; $uid = $_POST["fb_sig_user"]; } else { $isAddedUser = false; } if ($isCanvasUser || $isAddedUser) { // We have the uid for the current user, so we can use it to get the user's friends $info = $api->users->getInfo($uid); echo "You are:<br />"; echo (string)$info->user->uid . ": "; echo (string)$info->user->first_name . " "; echo (string)$info->user->last_name . "<br /><br />"; // A list of friend uid's, can also be gotten from $_POST but we wouldn't be able to show a call $uids = $api->friends->get($uid); $fql = 'SELECT uid, first_name, last_name FROM user WHERE uid IN (' . implode(',', $uids) . ')'; $result = $api->fql->query($fql); $friendCount = 0; echo "Here are up to twenty of your friends: <br />"; foreach ($result->user as $user) { if ($friendCount>=20) break; // Print only users who have authorized their data; those who have not will have empty name data if (!empty($user->first_name)&&!empty($user->last_name)) { // Print without uids, for individual user privacy echo (string)$user->first_name . " " . (string)$user->last_name . "<br />"; $friendCount++; } } } } } catch (Services_Facebook_Exception $e) { echo $e->getLastCall() . "<br />"; echo $e->getMessage(); } // See: http://wiki.developers.facebook.com/index.php/Authorizing_Applications echo "<fb:else>"; echo "<fb:redirect url=\"http://www.facebook.com/login.php?v=1.0&api_key="; echo Services_Facebook::$apiKey; echo "&next=http://apps.facebook.com"; echo $_SERVER['PHP_SELF']; echo "&canvas=\"/>"; echo "</fb:else>"; echo "</fb:if-is-app-user>"; ?>

Unofficial PHP4 Client Library

  • Description and download link can be found at [1].
  • It should be noted that there is an official PHP4 client lib now included with the PHP5 client. It's inside the "php4client" folder within the PHP5 client. (see above) However, this folder is currently only present in the pre-profile-redesign client,


Unofficial PHP5 FB Utils Class

Here's a class I've put together to handle a bunch of facebook functions (I am using it to support the Dvolver MovieMaker application). It is very much a work in progress (I'm sure there are better ways to do a bunch of the things I'm doing). Feel free to make improvements and please send those back to me and I'll test and repost. There's a logging function in the class which you can just comment out.

Note: Please give us a description of some of the things your class does EyeRmonkey 11:17, 12 June 2007 (PDT)


Sending an Invitation To your Application Example

  • Sample code form for the invite where fbuser is the user's facebook user id (barce@dogster.com):

As said here: Notifications.sendRequest this code is not valid anymore.

Sample PHP5 Code for Handling the Invite Form Submission

$facebook = new Facebook($api_key, $secret); $facebook->require_frame(); $fbuser = $facebook->require_login(); if (!empty($_POST['friend_selector_id'])) { $invite = 1; // we want to send an invite; use 0 for a request. $a_toids = array($_POST['friend_selector_id']); // an array of user_ids $invite_url = $facebook->api_client->notifications_sendRequest( $a_toids, 'application', // can be event or application, or maybe any string // fbml below '<fb:req-choice url="http://apps.facebook.com/dogster_cute/" label="Add Dogster Cute" /></fb:req-choice>', // an image to go with the invite -- mandatory parameter that cannot // be empty 'http://files.dogster.com/images/dogster/nav/top/tnd_logo.gif', // boolean for invite $invite); $invite_text .= '<fb:profile-pic uid="' . $_POST['friend_selector_id'] . '"></fb:profile-pic> '; $invite_text .= 'You are inviting ' . $_POST['friend_selector_name'] . '.<br/>'; $invite_text .= '<a href="' . $invite_url . '"> Finalize your invitation if you\'re sure about the invite.</a><br/>'; // print out $invite_text in your template }


<?php /* include the PHP Facebook Client Library to help with the API calls and make life easy */ require_once('facebook/client/facebook.php'); /* initialize the facebook API with your application API Key and Secret */ $facebook = new Facebook('f28cb2d2e3d798a9ec858e9e6131e172','fdfdretae44ca6fd56re3de07234fgh1hd'); /* require the user to be logged into Facebook before using the application. If they are not logged in they will first be directed to a Facebook login page and then back to the application's page. require_login() returns the user's unique ID which we will store in fb_user */ $fb_user = $facebook->require_login(); /* now we will say: Hello USER_NAME! Welcome to my first application! */ ?> Hello <fb:name uid='<?php echo $fb_user; ?>' useyou='false' possessive='true' />! Welcome to my first application!


<?php /* We'll also echo some information that will help us see what's going on with the Facebook API: */ echo '<pre>Debug:', print_r($facebook,true), '</pre>'; ?>

See Also

reference