Facebook

You are currently browsing articles tagged Facebook.

Me and Sandaruwan wanted to create a Facebook app, so just to learn the process I spent an hour or two to create a simple “Hello World” like app. I wanted to share the experience (a walk through / tutorial). It was pretty easy with the example code Facebook provides, and their developer forum.

Here is the link to my Hello World app http://apps.facebook.com/xvpj_hello/. You can download the source code here.

First install get the developer app: http://www.facebook.com/developers/. Then you can create a new application by clicking Set Up New Application.

Then under My Applications you can see a link to view example code.

Clicking it will show you an example index.php (which we are going to use) and a link to download the Facebook Platform PHP library. Download it and extract the folder php in the archive in your server (you need to host this app somewhere – you cannot test it on localhost).

I extracted it on www-root/fb/; i.e. an ls should look like

Note that index.php and invite.php are added later (it’s our code :) )

Then you need to goto Edit Settings in My Applications window

There under Canvas set the Canvas Callback URL to where you hosted the app. In my case it is http://xvpj.net/fb/. (note the ‘/‘ at the end, this is required otherwise there’ll be an error when navigating to the invite page). Also I set the render method to FBML.

Now to the good part, time to code :) . Here’s what my index.php looks like. Note that the link to invite.php includes a parameter invite. This is a small trick so that when user skips sending invitations, there is no parameter invite and the page is redirected back to index.php.

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
<?php
 
require_once 'facebook.php';
 
$appapikey = 'API_KEY';
$appsecret = 'SECRET';
 
$facebook = new Facebook($appapikey, $appsecret);
$facebook->require_frame();
$user_id = $facebook->require_login();
 
/* Greeting */
echo "<p>Hello, <fb:name uid=\"$user_id\" useyou=\"false\" />!</p>";
 
/* Get the list of friends */
$friends = $facebook->api_client->friends_get();
 
/* A link ot invite friends to the application */
echo "<p><a href=\"invite.php?invite\">Invite Friends</a></p>";
 
/* Printing out the friend list */
echo "<h2>Your friends</h2>";
 
foreach ($friends as $friend) {
  echo "<p><fb:name uid=\"$friend\" /></p>";
}

This is the invitation page I used (invite.php)

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
<?php
require_once 'facebook.php';
 
$appapikey = 'API_KEY';
$appsecret = 'SECRET';
 
$facebook = new Facebook($appapikey, $appsecret);
$facebook->require_frame();
$user_id = $facebook->require_login();
 
/* If invitations were sent */
if(isset($_POST["ids"])) {
?>
<center>
  Thank you for inviting <? echo sizeof($_POST["ids"]); ?> of your friends to <b><a href="http://apps.facebook.com/xvpj_hello/">Hello World</a></b>.
</center>
<br /><br />
<?php
}
/* If invite page is to be displayed */
else if(isset($_GET["invite"])) {
  /* A facebook query to select the friends of the current user who use the application */
  $fql = 'SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1='.$user_id.') AND is_app_user = 1';
 
  /* Execute the query */
  $_friends = $facebook->api_client->fql_query($fql);
 
  /* Get the user ids */
  $friends = array();
  if (is_array($_friends) && count($_friends)) {
    foreach ($_friends as $friend) {
      $friends[] = $friend['uid'];
    }
  }
 
  /* Convert the array of friends into a comma-delimeted string. */
  $friends = implode(',', $friends);
 
  /* Prepare the invitation text that all invited users will receive. */
  $content = "<fb:name uid=\"".$user_id."\" /> has invited you to use <a href=\"http://apps.facebook.com/xvpj_hello/\">Hello World</a>!\n";
  $content .= "<fb:req-choice url=\"".$facebook->get_add_url()."\" label=\"Add Hello World to your profile\"/>";
 
  /* This creates the form to select and invite friends.
   * The multi-friend-selector lets you search and select friends */
?>
  <fb:request-form action="invite.php" method="post" type="Hello World" content="<? echo htmlentities($content); ?>">
    <fb:multi-friend-selector actiontext="These poor lads still don't use this great app. Invite em all ;)" exclude_ids="<? echo $friends; ?>" />
  </fb:request-form>
<?
}
/* If sending invitations was skipped */
else {
?>
<fb:redirect url="http://apps.facebook.com/xvpj_hello/" />
<?
}
?>

Note: substitute API_KEY and SECRET

Tags: , , ,

Since it took me so long to find the wall of a huge messy profile of a friend when I wanted to say Happy Birthday, I was thinking of doing it by a simple HTTP POST.

First you need to gather following information

  1. Your post_form_id
  2. Your profile id
  3. Your friend’s profile id

That’s all you need, and you must be logged in to your facebook account (doesn’t matter if you have checked remember me).

Then type in the following html code and save it as a .htm file and double click on it. That’s it your message will be on your friends wall :) .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<html>
 <body>
  <form id="fb_wall" name="fb_wall" method="post"
   action="http://www.facebook.com/wallpost.php?id=[YOUR FRIEND'S ID]">
 
   <input type="text" id="to" name="to" value="[YOUR FRIEND'S ID]" />
   <input type="text" id="wall_text" name="wall_text"
          value="Happy Birthday" />
   <input type="text" id="from" name="from" value="[YOUR ID]" />
 
   <input type="text" id="post_form_id" name="post_form_id"
          value="[YOUR post_form_id]" />
 
   <input type="submit" />
  </form>
  <script language="JavaScript">document.fb_wall.submit();</script>
 </body>
</html>

Ok, so how do we find the post_form_id and the two profile id’s. To find the post_form_id you need to go to your profile and click view source in the view menu of the browser then search for post_form_id.

Now the two ids; Go to your profile and your friend’s profile and you’ll find the id in the URL.

That’s it! Try it for yourself.

Tags: , , ,