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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | <?php require_once 'facebook.php'; $appapikey = 'xxxxxxxxxxxxx'; $appsecret = 'xxxxxxxxxxxxx'; $facebook = new Facebook($appapikey, $appsecret); $user_id = $facebook->require_login(); $friends = $facebook->api_client->friends_get(); echo "<p>Hello <fb:name uid=\"$user_id\" useyou=\"false\" linked=\"false\" firstnameonly=\"true\"></fb:name>, 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 "<ul><li>Males: $male%</li><li>Females: $female%</li></ul>"; $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[caption] = "{*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 "<br /><fb:prompt-permission perms=\"publish_stream\">Publish results on your wall!!</fb:prompt-permission>"; } 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.















(11 votes, average: 4.00 out of 5)









This post has 21 comments
Lartar
I like the idea of step by step facebook app, but i think one of the most difficult stuff of the facebook api is the invite.php stuff. The ability to invite others users to your app, is a really big task. If you make one next time, i would give you 5 stars. ajajja
Regards,
Lartar
John
Great series – thanks very much for writing these
Roj
Thanks! Could you please add a complete tutorial that explains how you can put a flash game on facebook that posts on your profile “… did play the game and achieved Y” for an ingame event Y. And it would be perfect if ingame friend invitation was possible as well by loading your friends’ profile pics. That would be great!
Paul
Thanks for the Tutorials.
Could’ you maybe make one for a Flash Application with sending Feeds to the Facebook site?
That would be great because I couldn’t find any tutorials about it.
Esmin
Hi, what do you consider to be best practice in monetizing facebook applications?
I know google ads can be tweaked-in, but was wondering about your thoughts on this?
btw, great blog!
Developing a Facebook Application for absolute beginners – Step 4 : Emanuele Feronato
[...] are using the invitation form in a polite way, this is what you should add to the script seen in step 3 after the Php close tag ?> 60 61 62 63 64 65 66 <fb:request-form [...]
Murat Aktihanoglu
Would this work for any app or does the app have to be “white-listed” by Facebook?
thanks!
Emanuele Feronato
any app :)
Developing a Facebook Application for absolute beginners – step 5 : Emanuele Feronato
[...] Step 3: Publishing text, links and images on the user’s wall [...]
thomas
Great and very helpful. For some reason this step isn’t working for me however. When I click ‘Publish to Wall’ and then allow it nothing happens. Not sure if this is a live step and its simply not able to publish a wall but step 4 works so I would think this would. Except I can’t use the ‘skip’ feature in step 4 to make the box go away. And can’t figure out how to make it come with onclick instead by default. Sad we can’t use JQuery.
Salvo
Ciao Emanuele,
is it possible to publish a “pic_square” in someone’s wall using this app you wrote?
I mean..
I’ve a friend’s id and I want to put his picture on someother friend’s wall, using “$facebook->api_client->stream_publish($message,$attachment);”
So, just a question:
- Is it possible to add and send two or more images using the code that you wrote?
Grazie, ciao!
Deepak
Hello friends……..
i want to insert more than one media type(i.e image and vedio) in my facebook application how it is possible??
it take only one media type……
How can i solve my problem
Maddy
Dude…
That was a good Guide for beginners… helped me a lot…
additionally, can you come out with an example on how to publish a feed on friend’s Wall instead of publishing on our own wall?
now that facebook moving to stream.publish, my App is just became absolute mess. :(
Eddie L
I try and get this >>
Fatal error: Call to undefined method FacebookRestClient::users_hasAppPermission()
Don’t know what’s wrong?
chandrasekar
Hi,
Can you please help me to fix the issue, I have a strange problem, when I publish a story using the stream publish, only message part is showing in my feed as well as my friends feed, Even though i have given necessary stream publish permission, the attachment and actionlink are not showing up.
When i use your code, Only the message part is posting and i am not getting any errors, whereas if I use javascript Facebook.streamPublish then it is posting everything, but i need in PHP. Even I have update new client PHP.
Thanks in Advance
Chandu
Amit
hey trying this code computes the number of friends but does no shows the permission frame also my name is not displayed as”Hello “…guide me wat may be wrong…
thnx
Dan
Thank you SO much!!!! You’re a saint for doing this. I’m a beginner and this has helped me tremendously!
Mosquito
This tutorial helped me a lot. Thank you!
But I got this message when I try to post on the wall:
Call to undefined method FacebookRestClient::stream_publish() in /home/XXXXX/public_html/facebook/index.php
Could anybody tell me what is that meaning and why do I get the same error every time I try to publish something on the wall?
shevy
Did you ever figure this out?
cause I get the same error.
Thanks
Udar
Is this because facebook change API to new version including new auth method… I can not figure out new way, someone updated the php source with new code?
thanks for great articles anyway!
Vi?t Long Plaza
thanks for your post. I’ll try build my FB app