Facebook Apps: PHP Invite Code
August 29th, 2007
One thing I needed when making my second facebook application is a simple way for users to invite your friends. I found a few examples, but none of them were really what I was looking for. This code uses the ajax multiple friend select box(<fb:multi-friend-input>) for inviting friends instead of a long list or a random list of all your friends. You will need facebook.php and facebookapi_php5_restlib.php from facebook_client.tar.gz. If you want to see this invite code in action add the need numbers application. Here is the code:
<?php
/////////////////////////////////////////////
// These are the variables you need to change
/////////////////////////////////////////////// your app api key
$appapikey = ‘changeme’;
// your app secret
$appsecret = ‘changeme’;
// your callback url
$appcallbackurl = ‘http://website.tld/’;
// your facebook url of this page
$inviteurl = ‘http://app.facebook.com/app/invite.php’;/////////////////////////////////////////////
// This is the beginning of the section which includes and set up the php5 rest lib
/////////////////////////////////////////////require_once ‘facebook.php’;
$facebook = new Facebook($appapikey, $appsecret);
$user = $facebook->require_login();//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);
}/////////////////////////////////////////////
// These are the variables may want to change
/////////////////////////////////////////////
// The title of your Invite message
$title = ‘You have been invited’;
// The body of your Invite message
$text = ‘<fb:name uid=”‘.$user.’” firstnameonly=”true” /> invited you to join this app. <fb:pronoun uid=”‘.$user.’” /> wants you to join.<fb:req-choice url=”‘.$facebook->get_add_url().’” label=”Add it” />’;
// The logo associated with your invite message
$logo = ‘http://website.com/logo.gif’;
// Success message for when invites succeeded
$iworked = ‘<fb:success><fb:message>Your invitations have been sent</fb:message></fb:success>’;
// Error message for when all invites have been used
$ierror = ‘<fb:error><fb:message>You are out of invites, try again tomorrow</fb:message></fb:error>’;/////////////////////////////////////////////
// This is the beginning of the section that processes the invites
/////////////////////////////////////////////
if ($_POST[’ids’]) {
// Invite the friends that were selected.
$friends = array();
foreach ($_POST[’ids’] as $value) {
if (count($friends) < 10) {
$friends[count($friends)] = $value;
}
}
$tail = ‘&canvas’;
$url = $facebook->api_client->notifications_sendRequest($friends, $title, $text, $logo, true);if(!$url){
// If the maximum number of invites has been reached
$facebook->redirect($inviteurl.”?i=1″);
exit;
}else{
$facebook->redirect($url.$tail);
exit;
}
}if ($_GET[’sent’] == 1) {
// Display a message letting the user know invitations have been successfully sent.
echo $iworked;
}if($i==”1″){
// Error message for when all invites have been used
echo $ierror;
}/////////////////////////////////////////////
// This is the beginning of the actual invite form
/////////////////////////////////////////////
echo ‘<div style=”margin:20px”>
<h2>Invite some Friends</h2>
<form action=”‘.$inviteurl.’” method=”post”><fb:multi-friend-input width=”350px” border_color=”#8496ba” /><input type=”submit” value=”Invite” /></form>
</div>’;?>
theROC said:
Thank you soo much ………
minardimedia said:
The code is wring it wont work until you correct al the ‘ for ‘