Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Facebook and Php.

It’s time to add some features to the application we created in step 1.

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

The new script is this one:

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"; $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); } ?>

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:

$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 "
Publish results on your wall!!"; } 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:

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.

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