Create a Facebook application like “Friend interview” – Step 3
Here we go with the 3rd step. In this tutorial you will learn how to publish a full featured feed post to your friends’ wall, and to handle “umlauts” characters like “äöü” for the pleasure of our German readers, and for every user who has friends whose names contain special characters.
This is the kind of feed you are going to publish:

And this is the link of the application, just in case you want to have a look: About you Facebook application
The code is almost similar to the one we used in step 2:
|
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 59 60 61 62 63 64 65 |
<?php include_once "facebook.php"; $facebook = new Facebook(array('appId'=>'157934074246374','secret'=>'dontstealmysecretkey','cookie'=>true,'domain'=>'gamesalsa.com')); $session = $facebook->getSession(); if(!$session){ $url = $facebook->getLoginUrl(array('canvas'=>1,'fbconnect'=>0,'req_perms'=>'publish_stream','next'=>'http://apps.facebook.com/about_you_game/','cancel_url'=>'http://apps.facebook.com/about_you_game/')); echo "<p>Redirecting to permission request...</p>"; echo "<script type=\"text/javascript\">top.location.href = '$url';</script>"; } else{ if($_POST[to_do]=="Answer and publish"){ $ch=curl_init(); curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/$_POST[user]/feed"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1) ; curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=$session[access_token]&message=".urlencode("just answered a question about you:")."&link=".urlencode("http://apps.facebook.com/about_you_game/")."&picture=".urlencode("http://www.gamesalsa.com/aboutyou/about_you_icon.png")."&name=".urlencode("Do you think $_POST[name] likes this app ?")."&caption=".urlencode("and the answer is...")."&description=".strtoupper(urlencode($_POST[answer]))."&actions=".urlencode("{\"name\": \"Play now\", \"link\": \"http://apps.facebook.com/about_you_game/\"}")); $result = curl_exec($ch); curl_close($ch); } $friends = $facebook->api('me/friends'); $number_of_friends = count($friends[data]); $random = rand(0,$number_of_friends-1); $friend_id = $friends[data][$random][id]; $friend_name = $friends[data][$random][name]; $img = "http://graph.facebook.com/$friend_id/picture?type=large"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> </head> <body> <div style = "width:513px;background-color:#edeff4;background-image:url('<?php echo $img; ?>');background-repeat:no-repeat;padding:5px 5px 5px 210px;font:normal 16px verdana"> Do you think <span style = "color:red;fnt-weight:bold"><?php echo $friend_name; ?></span> likes this app? <form method = "post" style ="margin-top:20px"> <textarea name = "answer" style = "border:1px solid #bdc7d8;width:100%;height:100px;font:normal 15px verdana"></textarea> <input type = "hidden" name = "user" value = "<?php echo $friend_id;?>" /> <input type = "hidden" name = "name" value = "<?php echo $friend_name;?>" /> <input type ="submit" value = "Answer and publish" name ="to_do"/> <input type ="submit" value = "Just answer" name ="to_do"/> </form> </div> <script type="text/javascript"><!-- google_ad_client = "pub-2062493463858889"; /* about you */ google_ad_slot = "0401513269"; google_ad_width = 728; google_ad_height = 90; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </body> </html> |
Using proper html tags such as http-equiv at line 38 allow us to properly render special characters, while the key of this post is line 20, where we pass a lot more information than in step 2.
Let’s see it in detail:
access_token: it’s the access token required to publish on friends’ wall. Its value must be
|
1 |
$session[access_token] |
message: it’s the message which will appear next to your name. I used
|
1 |
urlencode("just answered a question about you:") |
because I wanted my friend to see “Emanuele Feronato just answered a question about you:”. I also used urlencode although it’s useless in this case, just not to forget to encode the string should I change it in the near future with another string with some special characters.
picture: the absolute URL of the picture to display on the left of your post.
|
1 |
urlencode("http://www.gamesalsa.com/aboutyou/about_you_icon.png") |
name: it’s the name of the link you are publishing in the feed. This is where we will place the question.
|
1 |
urlencode("Do you think $_POST[name] likes this app ?") |
link: it’s the link attached to the picture and the name. You may want to use the URL of the application itself.
|
1 |
urlencode("http://apps.facebook.com/about_you_game/") |
caption: the caption of the link, placed just under the link itself. Don’t write anything important here as it won’t be noticed that much since the blue color of the link grabs almost all attention. Perfect to write something like “and the answer is…”
|
1 |
urlencode("and the answer is...") |
description: it’s the description of the link and it’s the longest text you can enter. We’ll place the answer there.
|
1 |
strtoupper(urlencode($_POST[answer])) |
actions: you can also add a custom action to the action footer of the post (where you can find “Like”, “Comment”, and so on). Actions should be added as a JSON list containing “name” and “link”
|
1 |
urlencode("{\"name\": \"Play now\", \"link\": \"http://apps.facebook.com/about_you_game/\"}") |
And here you, go, have a try with the application: About you Facebook application.






(5 votes, average: 3.80 out of 5)








This post has 24 comments
Kyle
I have really liked this series about the facebook app. I really appreciate it.
Is there are real difference between using curl to post to the users wall or using the graph api post?
Michael Bochniak
Thank you! Great Tutorial that really works!
Callum
@Kyle, you need to use cURL to post with the Graph API. This is the Graph API
hadadjus
think you so much , i liked this tutorial but please can you post how can”i make a list of question ? a question by user
and how can i choose friend to ansower more question ?
Michael Bochniak
Hi there! I noticed a small problem. You use following code
For german users they use another VALUE like “Veröffentlichen” with “umlauts”.
I made it otherwise i asked for a hidden value
and it still works. but the other button would do the same, so i programmed it
but the problem with reload is…when someone typed a message previously and then wants to skip the next question the browser wants to load the $_POST data again… is there another way without 2 submit buttons in the form??
Michael Bochniak
nice…the html tag
doesn't work -.-Danielo
Thx Emanuelle!
I made it :D
http://apps.facebook.com/the_sheperd/
SSH
How to use action link in ‘POST’ graph api using fbconnect sdk(iphone)
Jorgen
My app wont post. help?
Jorgen
This code is broken
Can someone please fix it?
Jorgen
Sorry for posting 3 times in a row, but Your app posts the right way, but mine doesn’t post. What are the settings that you have for your app or did you change the code. Thank You!
hadadjus
please ansowr me ; i neet your help
Nalc
So far so good! I made some changes and managed to get a random question! There is only one problem is that whenever I enter the application always enjoyed the phrase forward for permission, there is no way around this? And I have another problem, Adsense does not appear in IE. Why?
nithin
hey emanuele, nithin here :)
this code is not posting on friends wall.
please help, please please
Kasper
Code is not working. It post only on first time a user authorizes it, and not after that.
Somehow it is losing the $session…
Kasper
I have found where the issue is:
AFTER the first failed attempt to post, it works. But never on the first try.
Denise
Yes, I also noticed this problem – so how to fix it? Emanuele please help :-)
Bill
$session will continually be lost and refound if using IE and not using proper P3P header. Make sure that you incorporate that into your appy :).
Johny
Not working, don’t bother.
Author seems to have abandoned this.
John
@Bill don’t say bullshit just for the sake of saying something that would make you look smart. What you are referring to only affects ie6. What’s is wrong with this code is much more complex.
gecko
really simple facebook apps.
I love this series, make more facebook apps tutorial series please..
steven
tell me is possible post on the wall of all my friends?
i mean no one by one
all of them?
i hope you can help me with that
sorry i dont speak english very good i hope you understand me and help me
thanks
pedro
SHAME!
Facebook seemed to have changed everything since 2010
and I am getting an error that
$Facebook->getSession()
no longer exists ??
[21-Nov-2011 00:25:00] PHP Fatal error: Call to undefined method Facebook::getSession() in /home/mbanorg/public_html/fb/friendstate/index.php on line 7
sid
App is working nicely but not being posted on friendz wall ?
what should i do plz guide mee…….