Developing a Facebook Application for absolute beginners – step 3

In step 1 we created a simple Facebook application, and in step 2 we made our application able to write on the wall

Now it’s time to add images and more text to users’ wall when they run the application.

This is what I am creating:

I know I wrote “appliction”… but it was a typo… don’t make me take another screenshot (lazy geek…)

In order to do this, you will need to know how to include attachments.

You can add a lot of rich information to a post by including an attachment. The attachment gives you the opportunity to expand on the post by describing what the user did in your application.

This is the script:

require_login();

$friends = $facebook->api_client->friends_get();

echo "

Hello , you have ".count($friends)." friends"; foreach($friends as $friend){ $infos.=$friend.","; } $infos = substr($infos,0,strlen($infos)-1); $gender=$facebook->api_client->users_getInfo($infos,'sex'); $gender_array = array(); foreach($gender as $gendervalue){ $gender_array[$gendervalue[sex]]++; } $male = round($gender_array[male]*100/($gender_array[male]+$gender_array[female]),2); $female = 100-$male; echo "

  • Males: $male%
  • Females: $female%
"; $message = "has ".count($friends)." friends. $male% of them are male. $female% are female"; $image = array(); $image[type]="image"; $image[src]="http://www.gamemummy.com/facebook/genderz_demo/genderz.png"; $image[href]="http://apps.facebook.com/genderz_demo/"; $attachment = array(); $attachment[name] = "Try the appliction!!"; $attachment[href] = "http://apps.facebook.com/genderz_demo/"; $attachment = "{*actor*} has ".count($friends)." friends"; $attachment[description] = "powered by genderz_demo"; $attachment[media] = array($image); $has_permission = $facebook->api_client->users_hasAppPermission("publish_stream"); if(!$has_permission){ echo "
Publish results on your wall!!"; } else{ $facebook->api_client->stream_publish($message,$attachment); } ?>

Line 37: declaring an array called $image… it will store all image information such as type (line 38), source (line 39) and link (line 40)

Line 42: declaring an array called $attachment that will store all attachment information.

You can save a lot of information in the array by using these fields, that’s what I used:

name: The title of the post (line 43). The post should fit on one line in a user’s stream; make sure you account for the width of any thumbnail.

href: The URL to the source of the post referenced in the name (line 44) . The URL should not be longer than 1024 characters.

caption: A subtitle for the post that should describe why the user posted the item or the action the user took (line 45) . This field can contain plain text only, as well as the {*actor*} token, which gets replaced by a link to the profile of the session user. The caption should fit on one line in a user’s stream; make sure you account for the width of any thumbnail.

description: Descriptive text about the story (line 46). This field can contain plain text only and should be no longer than is necessary for a reader to understand the story.

media: Rich media that provides visual content for the post (line 47) . media is an array that contains one of the following types: image, flash, mp3, or video, Make sure you specify only one of these types in your post. I attached the $image array, so my media is an image.

Finally at line 55 I send both the text created at step 2 and the attachment to the wall.

Take a look at the application and see how does it publish your friends gender stats on your profile.

Next time, some more improvements and a chart, as suggested by a reader.

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

215 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
// Stairs
// 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