September 8, 2012 2

Force users to LIKE your fanpage – Facebook Flash AS3 & PHP, Part4

By in Facebook API, Freebies, PHP

Today, I want to show you how to enable the application on fanpage and how to force users / players to like fanpage before they can use it. Don’t belevie that your agency will get new clients because you have made great and fun application or game. It’s all about:

  • New users
  • Quantity of Likes

Updated demo here >>

This time we use exclusively PHP goodness. But first things first. To create your own fanpage go here . Now let’s implement some changes in the application settings. Enable Page Tab and fil it with the same values as App on Facebook area.

Page Tab Facebook Settings

Page Tab Facebook Settings

Cool , now let’s change Website with Facebook Login. For security reasons, Facebook redirects only to this url. Previously, users were guided to the address of the canvas, and now we want them to be redirected to pagetab url.

Now that we have it we can move on to adding application to your newly created fanpage. All you need to do is to run this url:  https://www.facebook.com/dialog/pagetab?app_id=YOUR_APP_ID&next=YOUR_URL  where:

  • YOUR_APP_ID – is your app id :)
  • YOUR_URL – is your App’s Canvas URL

You should see something like this:

Add Facebook Page Tab

Add Facebook Page Tab

Great, now let’s moveto the main parts of this post. We will implement functionality that allows us to check user whether he is a fan of our page If so, run the application, if not, we will show image with an information to click Like button.

The magic happens at the beggining of index.php file:

<?php

     require_once("facebookPHP/facebook.php");
     $config = array(
          'appId' => 'YOUR APP ID,
          'secret' => 'FILL YOUR APP SECRET'
     );
     $facebook = new Facebook($config);
     try {
          $signedRequest = $facebook->getSignedRequest();
          $liked = $signedRequest['page']['liked'];
          $appData = $signedRequest['app_data'];
     }
     catch(FacebookApiException $e) {
          error_log($e->getType());
          error_log($e->getMessage());
     }
?>

Note that require_once(“facebookPHP/facebook.php”) is refering to new facebook.php file that you will find in source files package. Of course the most important value for us is $liked  Boolean and we make use of it at the bottom like this:

<?php if( $liked ){
      ?>/// SHOW APPLICATION
<?php
}else{
     ?>// SHOW IMAGE
<?php
}
?>

Ohhh… Remember to change your redirect_uri in FB.getLoginStatus function to your new page tab url. Now go and collect Likes like a master!

Source files >> FB / PHP Part4 – Force user to like your fanpage (802)

September 3, 2012 0

Friends Invitation – Facebook Flash AS3 & JS Application, Part3

By in Facebook API, Freebies, Tutorials

In the last part I showed you how to send a post on the wall. Today I would like to introduce you another very important method in Facebook API – how to invite friends.

Updated demo here >>

There are very few methods that without a good support of content are able to generate traffic and interest in your application. Invite friends is one of those for sure.

As soon as you click invite friends button one of  Javascript functions is triggered. Let’s take a closer looke at that:


function inviteFriends(arg) {
     FB.ui({
          method : 'apprequests',
          title : arg.title,
          message : arg.message,
          filters : arg.filters,
          exclude_ids : arg.exclude_ids,
          max_recipients : arg.max_recipients
     },
     function(response) {
          var fla = document.getElementById('Main');
          if (response && response.to) {
               console.log('User invited at least 1 friend.');
               console.log(response);
          } else {
               console.log('User canceled invitation.');
               console.log(response);
          }
     fla.invitationCallback(response);
     });
 }

The first part is a request to Facebook and as soon as it run the pop up shows up. As you can see I am passing some important arguments in here:

  1. title  - the title for the Dialog (optional). Max length is 50 characters.
  2. message  - the Request string the receiving user will see. It appears as a request posed by the sending user. The max length is 60 characters. The message value is displayed in Notifications.
  3. filters  -  (optional) default is ”, which shows a Multi Friend Selector that includes the ability for a user to browse all friends, but also filter to friends using the application and friends not using the application. Can also be allapp_users and app_non_users. This controls what set of friends the user sees if a Multi Friend Selector is shown.
  4. exclude_ids - array of user IDs that will be excluded from the Dialog
  5. max_recipients - an integer that specifies the maximum number of friends that can be chosen by the user in the friend selector.

The second part is responsible for the callback, as soon as you send an invitation to friends or give up activities, we get the information here and pass it back to AS3.

Cool, let’s move to Flash now. Compared to previous versions of files I introduced a few changes. First, in FacebookService class i have implemented  new method inviteFriends. Nothing special, just running discussed earlier Javascript function and passing all needed arguments. I’ve also added few new items in UI. We have a invite buton, filters combo box and numeric stepper to set max_recipients  value. I’ve also created InviteFriendsVO which holds all arguments I want to pass to my invitation request.

In general, everything is quite clear. I would like to focus on one  special thing. If I use a filter combo box, why use an additional exclude_ids? Imagine the situation that your application is a contest where you can win prizes. You can play only once a day and receive extra chance  in the return of  inviting at least 5 friends to the game. Good idea to increase the number of players and stimulate traffcu, right? But we have a problem here. Filters will not be helpful because it operate sonly when the user is already a member of the application, when accepted the invitation. Therefore, the potential player can invite the same people (as long as they do not accept the invitation) in exchange for another chance and spamming  innocent beings. Here comes the power of exclude_ids. This requires from us a bit more work, but it’s definetely worth that. In the sample application I am using the SharedObject array to store the users ids to which the invitation was sent. I receive that list from  javascript callback and immediately save them to a SharedObject. The next time you call the function I am taking those saved ids and pass them as exclude_ids parameter. Pretty clever. Of course in a real world app you probably will not use SharedObject but some server side technology to keep that data as safe as possible.

So – no more waiting for the acceptance of the invitation.

That’s it.

Source files >> AS3 / JS Part3 – Friends Invitation (814)