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:

'157934074246374','secret'=>'sorrythissecretkeywontwork','cookie'=>true,'domain'=>'gamesalsa.com'));
 
$session = $facebook->getSession();
 
if(!$session){
    $url = $facebook->getLoginUrl(array('canvas'=>1,'fbconnect'=>0));
    echo "

Redirecting to permission request...

"; echo ""; } else{ $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?

"; } ?>

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers