Attachment Example
From Facebook Developer Wiki
Important! As we announced on the Developer Blog, after the new Inbox launches to all users (a few weeks after August 11, 2009), we will deprecate this method for Publisher attachments. You should start using stream attachments for Publisher integrations.
This is a basic outline of how to set up attachments.
Basically, set the Attachment callback URL to some page on your server. For instance http://yourserver.com/attach.php
attach.php will handle both the attachments, and the selection of the attachments.
<?php
if (!array_key_exists('message_sent', $_POST) || $_POST['message_sent'] < 1)
{
// The user is selecting the attachment, in other words, preview mode
// Print out form elements that allow the user to select the attachment
// In this example, the attachment is simply a bit of text we will collect in the 'sample' input field
// If the 'sample' exists in _POST, then we are actually previewing. Else, we are here the first time
// to collect that text
if (array_key_exists('sample', $_POST))
echo '<fb:editor-text label="Sample" name="sample" id="sample" value="'. htmlspecialchars($_POST['sample']) . '"/>';
else
echo '<fb:editor-text label="Sample" name="sample" id="sample" value="type something here"/>';
// The following line tells facebook to use the same script for attachments, but you don't need to. You can use a seperate script.
echo '<input type="hidden" name="url" value="http://yourserver.com/attach.php" />';
// The following line lets your user preview the attachment they've selected. This is not required.
echo "<fb:attachment-preview>Click here to preview attachment</fb:attachment-preview><br /><br />";
echo "<b>Preview:</b>";
echo "<hr />";
}
else
{
// Do anything here you want to do for attached objects only
echo "(The object is attached)<br/><br/>";
}
// Display your attachment here. This will show up both for preview and live attachments
if (array_key_exists('sample', $_POST))
echo "You wrote <b>" . htmlspecialchars($_POST['sample']) . "</b><br />";
?>
When the user clicks your "attach" link, Facebook calls your script and displays it in a pop-up dialog like the one below. Each time the user clicks the "Preview" link, Facebook calls your attach script again with the values of the form inputs as POST variables, and updates the dialog with whatever your script returns.
"Preview" screenshot from the sample code
When the users clicks the "Attach" button, your script will be called with the POST variable message_sent set to 1, and uses your script's output as the attachment:
"Attached" screenshot from the sample code
You're given the user doing the attachment as fb_sig_user, but remember that users who don't have your app installed can still make attachments on their friends' walls (in this case you won't be given fb_sig_user).
Elements Needed/Steps
1) Go to My Applications and click on Edit Settings for your app.
- a) At the bottom, under Attachments, for "Attachment Action:" type in the text you'd like to appear in the wall attachment area. For example "Attach Photo" or "Slap x"
- b) In "Callback URL:" type in the URL to where you will house your code, i.e. attach.php. Include the full URL and have it be the URL of your site, not the apps.facebook.com URL. For example, http://www.yourserver.com/attach.php
2) On your callback for attachment page, you will need to have field inputs where the name is 'url' and the value is the URL to the page where you will display what they chose. For example:
<table width="100%" border="0" cellspacing="0" cellpadding="3"> <tr> <td><input name="url" type="radio" value="http://yourserver.com/facebook/attachment.php?actiontotake=choice1"> Choice One </td> </tr> <tr> <td><input name="url" type="radio" value="http://yourserver.com/facebook/attachment.php?actiontotake=choice2"> Choice Two</td> </tr> <tr> <td><input name="url" type="radio" value="http://yourserver.com/facebook/attachment.php?actiontotake=choice3"> Choice Three </td> </tr> </table>
NOTE: Do not wrap this in a form tag, Facebook is placing the above in their own.
3) On your attachment.php page, just test for which choice they made and display what the attachment should be accordingly.
NOTE: Wall attachments used to be displayed in a collapsed mode on page load, with a preview image specified by the fb:wall-attachment-img tag. However, wall attachments are currently displayed in full (just like message attachments), so the fb:wall-attachment-img tag has been deprecated.
