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.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (11 votes, average: 4.00 out of 5)
Loading ... Loading ...
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 21 comments

  1. Lartar

    on October 1, 2009 at 1:34 am

    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

  2. John

    on October 1, 2009 at 9:50 am

    Great series – thanks very much for writing these

  3. Roj

    on October 1, 2009 at 1:43 pm

    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!

  4. Paul

    on October 1, 2009 at 3:50 pm

    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.

  5. Esmin

    on October 7, 2009 at 12:06 am

    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!

  6. Developing a Facebook Application for absolute beginners – Step 4 : Emanuele Feronato

    on October 7, 2009 at 10:54 am

    [...] 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 [...]

  7. Murat Aktihanoglu

    on October 7, 2009 at 4:51 pm

    Would this work for any app or does the app have to be “white-listed” by Facebook?

    thanks!

  8. Emanuele Feronato

    on October 7, 2009 at 5:02 pm

    any app :)

  9. Developing a Facebook Application for absolute beginners – step 5 : Emanuele Feronato

    on October 9, 2009 at 6:16 pm

    [...] Step 3: Publishing text, links and images on the user’s wall [...]

  10. thomas

    on October 19, 2009 at 6:11 pm

    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.

  11. Salvo

    on November 10, 2009 at 12:49 pm

    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!

  12. Deepak

    on November 14, 2009 at 11:42 am

    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

  13. Maddy

    on November 23, 2009 at 7:11 pm

    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. :(

  14. Eddie L

    on January 31, 2010 at 10:26 am

    I try and get this >>
    Fatal error: Call to undefined method FacebookRestClient::users_hasAppPermission()

    Don’t know what’s wrong?

  15. chandrasekar

    on February 8, 2010 at 11:11 am

    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

  16. Amit

    on February 22, 2010 at 3:46 pm

    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

  17. Dan

    on March 20, 2010 at 4:00 pm

    Thank you SO much!!!! You’re a saint for doing this. I’m a beginner and this has helped me tremendously!

  18. Mosquito

    on August 8, 2010 at 9:56 pm

    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?

  19. shevy

    on October 17, 2010 at 1:29 pm

    Did you ever figure this out?
    cause I get the same error.
    Thanks

  20. Udar

    on October 22, 2010 at 9:14 am

    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!

  21. Vi?t Long Plaza

    on April 30, 2011 at 6:59 pm

    thanks for your post. I’ll try build my FB app