PHP
From Facebook Developers Wiki
Contents |
[edit] Official PHP Client Library
The package, available here and the new version for updated profile is available here. The package contains three directories and a README file.
- The client 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 README file for additional information.
[edit] 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.
[edit] Installation
$ pear install Services_Facebook
[edit] Usage
<?php
require_once 'facebook.php';
$appapikey = '[your api_key]';
$appsecret = '[your secret]';
$facebook = new Facebook($appapikey, $appsecret);
$user = $facebook->require_login();
//[todo: change the following url to your callback url]
$appcallbackurl = 'http://www.tventasnetwork.com/maps.html';
//catch the exception that gets thrown if the cookie has an invalid session_key in it
try {
if (!$facebook->api_client->users_isAppAdded()) {
$facebook->redirect($facebook->get_add_url());
}
} catch (Exception $ex) {
//this will clear cookies for your application and redirect them to a login prompt
$facebook->set_user(null, null);
$facebook->redirect($appcallbackurl);
}
?>
[edit] Unofficial Extension to PHP5 Client Library (Photo Upload Support)
NOTE: This code was not written by Facebook developers and is not endorsed by them in any way. It was written by Jeremy Blanchard and Paul Wells and is released under the GPL.
The current version of the PHP5 client does not have any built in support for photo upload. The extension provided here makes it easy to upload photos into Facebook albums.
[edit] 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)
[edit] 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)
[edit] 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 ($_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/>\n";
// print out $invite_text in your template
}
[edit] Notes
- Some IIS users have noticed that the URL redirect functions (e.g. require_login()) don't work properly. This may be caused by IIS not populating the value of $_SERVER['REQUEST_URI']. If this is the case, try changing all references to $_SERVER['REQUEST_URI'] to $_SERVER['SCRIPT_NAME'].
[edit] See also
- PHP Sessions
- Makebeta - Facebook PHP Tutorial
- PHP UTF-8 Cheatsheet - step-by-step instructions for ensuring your MySQL and PHP applications support international languages
