Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Facebook and Php.

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.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.