Developing a Facebook Application - Part 3

2
320

In Part 1 we created a simple Facebook application, and in Part 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:

Developing a Facebook Application - Part 3

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 = "{*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.

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.

2 মন্তব্য

একটি উত্তর ত্যাগ