Developing a Facebook Application - Part 2

3
455

It’s time to add some features to the application we created in Part -1

This time we’ll add some interaction with Facebook, such as publishing results on your wall.

The new script is this one:

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
<?php
 
require_once 'facebook.php';
 
$appapikey = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
$appsecret = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
 
$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";
 
$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);
}
 
?>

That is the same as Developing a Facebook Application for absolute beginners until line 33

So let’s take a look to the new lines:

35
36
37
38
39
40
41
42
43
44
$message = "has ".count($friends)." friends. $male% of them are male. $female% are female";
 
$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);
}

line 35: defining a simple message telling how many friends do you have

line 37: checking if the user allowed the application to publish on the wall. When you run a Facebook application, it cannot interact with your profile doing actions such as writing on your wall or sending emails.

Facebook offers some API functionality that requires the user to specifically opt in before your application or site can use that functionality. These methods are specific to certain use cases that require a greater level of trust from the user. Users express this trust by granting your application or site specific extended permissions.

The permission we need this time is publish_stream which lets your application post content, comments, and likes to a user’s profile and in the streams of the user’s friends without prompting the user.

You can check the whole list of permission at this page

line 40: if the user did not allow the application to publish, renders the content of the tag as a link that, when clicked, initiates a dialog requesting the specified extended permissions from the user.

This one:

Developing a Facebook Application - Part 2

Once you click on allow, and reload the application, the message at line 35 will be published on your profile thanks to…

line 43: stream_publish method publishes a post into the stream on the user’s or Facebook Page’s Wall and News Feed.

There are various options we’ll meet during next tutorials, but at the moment you are able to publish content on users profile.

Take a look at the application and see how does it publish your friends gender stats on your profile.

Next time, we’ll add even more interaction.

3 মন্তব্য

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