Create a Facebook application like “Friend interview”
Do you know Friend Interview?
It’s a Facebook application which asks a random question about a random friend of yours, and publish the result on his wall. I won’t publish a snapshot of such app since it’s full of rubbish and ads, but I am sure you know what I am talking about (and moreover there’s the link!).
You should know how to create a Facebook application, anyway I’ll post a recap here.
1) Go to http://www.facebook.com/developers/ and click on “Set Up New Application”.

2) Give a name to your application. You can give it any name as long as it does not infringe copyrights, agree to terms and click on “Create Application”.

3) On “Facebook Integration” panel give it a Canvas Page name (this must be unique), and in “Canvas URL” write the path on the folder on your server which will contain the application itself, set “Canvas Type” as “IFrame” and “IFrame Size” to “Auto-resize”, then press “Save Changes” and at the moment you are ready to create your application.

4) Download the Facebook API from GitHub and copy facebook.php in the folder you specified in the Canvas URL.
Now everything is ready to develop your application. In this first part, we will ask for permissions and choose one random friend.
Create a new file called index.php with this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php include_once "facebook.php"; $facebook = new Facebook(array('appId'=>'157934074246374','secret'=>'sorrythissecretkeywontwork','cookie'=>true,'domain'=>'gamesalsa.com')); $session = $facebook->getSession(); if(!$session){ $url = $facebook->getLoginUrl(array('canvas'=>1,'fbconnect'=>0)); echo "<p>Redirecting to permission request...</p>"; echo "<script type=\"text/javascript\">top.location.href = '$url';</script>"; } else{ $friends = $facebook->api('me/friends'); $number_of_friends = count($friends[data]); echo "<p>You have $number_of_friends friends</p>"; $random = rand(0,$number_of_friends-1); echo "<p>A random friend name: <strong>".$friends[data][$random][name]."</strong></p>"; echo "<p>A good question would be: do you think <strong>".$friends[data][$random][name]."</strong> likes this app?</p>"; } ?> |
Let’s see its meaning:
Line 3: including the required library.
Line 5: initializing the Facebook application. In the array you must pass the following elements:
appId: the application ID
secret: the application secret
cookie: (optional) boolean true to enable cookie support
domain: (optional) domain for the cookie
fileUpload: (optional) boolean indicating if file uploads are enabled
Line 7: getting the session object. At this point, the script splits in two paths, according to $session value.
$session is NULL (lines 9-13)
If $session is NULL, we need to redirect to the login url to let the player allow the application use his personal information. The redirection is made with a simple javascript, using getLoginUrl.
$session is not NULL (lines 14-21)
In this case I put in $friends array an array with all friends names and Ids, then I randomly pick one of them and display its name and a possible question.
And that’s it, this is what you’ll get:

You can test it by yourself here: http://apps.facebook.com/about_you_game/
Now you know how to pick a random friend. During next step you will be able to answer a question about him and post it on its wall.















(25 votes, average: 4.64 out of 5)









This post has 18 comments
Callum
Another fantastic tutorial, however when I try this in my own test application it does not ask me for permission it just goes straight ahead and tells me my friends info. Being new to the facebook API I’m not entirely sure why it does this.
Danielo
Hi Emanuele, i do it the tutorial but when i tested mi app, facebbok.php tell me an error.
Parse error: syntax error, unexpected T_NEW in facebook.php on line 4.
It’s strange…:(
kegogrog
When I did set up an app I encountered the problem that the server my index.php was hosted on was able to handle php 5 but I had to tell it directly by naming the file ‘index.php5′. Facebook didn’t find the file before but it worked then. Just in case someone may have that problem too.
Danielo
Can you upload the files without the App Secret?
Thx!
Danielo
Can you upload your files without the Api Key?
Thx
Smin Rana
I have successfully done it. Only problem i have After removing application from privacy control and try to click again the URL (apps.facebook.com/your-app) there is an Exception thrown “OAuthException”. You need to catch that error.
just replace the else statements from above tutorial with these lines:
/*************************************************/
try {
$friends = $facebook->api(‘me/friends’);
$number_of_friends = count($friends[data]);
echo “You have $number_of_friends friends”;
$random = rand(0,$number_of_friends-1);
echo “A random friend name: “.$friends[data][$random][name].”“;
echo “A good question would be: do you think “.$friends[data][$random][name].” likes this app?”;
} catch (FacebookApiException $e) {
$url = $facebook->getLoginUrl(array(‘canvas’=>1,’fbconnect’=>0, ‘req_perms’ => ‘publish_stream’));
echo “Please wait for authorization…”;
echo “top.location.href = ‘$url’;”;
}
/************************************************************/
Also if you need permission you can do that, add another attribut in the array.
$url = $facebook->getLoginUrl(array(‘canvas’=>1,’fbconnect’=>0, ‘req_perms’ => ‘publish_stream’));
hadadjus
please can you give me more helps ! i want to use more quesion , i hope to get an application this app
please heps me about input friend list
input class=”inputsubmit” name=”confirm” value=”Submit” onclick=’FBML.clickRewriteAjax
hadadjus
please how can ‘i add an image on the post to friend wall with ansewr
Creating my First Facebook PHP Application #6 | Facebook Answers
[...] got most of that code from the following article: Useful FB tutorial. I added the try.. exception.. bit as a direct result to one of the comments left on the article. [...]
sameer Singh
Directory Listing Denied
This Virtual Directory does not allow contents to be listed.
can sumbdy xplain me that error????
i am trying to connect to my company’s server but it keeps givinng that error
Christian Scholz-Flöter
kegogrog, thank you very much for your comment about using a “php5″ extension. Saved my neck!
Christian Scholz-Flöter
BTW, after doing all Emanuele tells us to do and after changing my index.php’s extension to php5 I got an error message — API Error Code: 191.
I managed to get past that by entering correct values for “Site URL” and “Site Domain” under “Web Site” in the “Edit settings” area.
Social Web Dev – Using the Facebook API « William Barnwell
[...] http://www.emanueleferonato.com/2010/10/20/create-a-facebook-application-like-friend-interview/ [...]
mahdi
thank you mate !
i have an old gift app
it stoped because facebook made some changes on the codes !
so i bought this script on 2008 and i’am a beginner on php and programming but i need to reactivate my application but i don’t know how ?
+i have the script !but it doesn’t work !!!
please is there any help ?
efriteokk@gmail.com
Haseebp
Superb..Very Good Post…~
I Love it..!
Wolfe
I really enjoyed this step-by-step. I can’t seem to figure out how to make random questions. Is there anything you can offer that would assist me with that dilemma?
gus
I followed the tutorial step by step
1. http://apps.facebook.com/about_you_game/ not working (infinite loop)
2. getting an error message trying to run my application
Parse error: syntax error, unexpected T_NEW in /home/content/g/b/y/gbyteworks/html/facebook/facebook.php on line 4
Ehtisham
Are all the API calls still valid? Like the one mention above. Or Facebook has made changes?