How to write a Facebook App?

Writing a Facebook is as easy as signing up for Facebook account. There is just one prerequisite, you must have some sort of web hosting, steps given below use PHP so a web hosting with PHP support is required, and you must know how to upload files. Follow these steps

  1. Sign up for Facebook if you don’t have an account. If you can’t do this simple thing do not read further.
  2. Go to Account > Applications and add Facebook Developer application.
  3. Go to the app page, locate and click the “Set up new application” button.
  4. Follow the instructions which let you name your app and set other things.
  5. Dont bother if you dont understand anything in the forms. Fill what you understand and just pay attention to following fields:
    a) Under Canvas > Canvas Page URL, create a Canvas URL which will be your app’s URL on Facebook, for example “my-hello-world-app”
    b) Under Canvas > Canvas Callback URL, this will be URL where we will host our Facebook app, so if I host my app on kumarchetan.com, the URL will be http://www.kumarchetan.com/, easy
    c) Under Canvas > Canvas Settings > Render Method, choose FBML.
    Save changes.
  6. Download the client library from here: http://svn.facebook.com/svnroot/platform/clients/packages/facebook-platform.tar.gz
  7. Extract the library on your local machine and you will end with a folder with two sub folders. We will be using folder named “php”.
  8. Open “index.php” in your choice of editor and you will see following code
    <?php
    // Copyright 2007 Facebook Corp. All Rights Reserved.
    //
    // Application: Ye app puri philmy hai
    // File: 'index.php'
    // This is a sample skeleton for your application.
    //
    require_once 'facebook.php';
    $appapikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; //Your API key
    $appsecret = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyy'; //and your secret ;-)
    $facebook = new Facebook($appapikey, $appsecret);
    $user_id = $facebook->require_login();// Login is required

    Delete everything after this code, we will simply say “Hello dear” with user name in our app.
  9. Add following
    echo 'Hello <fb:name uid="', $user_id, '" firstnameonly="true" />!';
  10. Upload all the files in php folder to the server which you provided in Canvas Callback URL.
  11. Go to URL http://app.facebook.com/my-hello-world-app, remember “my-hello-world-app” was Canvas URL for our app.
  12. Voila!!! Your app is ready!

This is a very very very basic app. You can add more interactivity to this app. You need to browse through http://developers.facebook.com/ and you can use my app here -> http://www.facebook.com/apps/application.php?id=111590402213823 which simply reads a flat file db of Bollywood dialogues and prints a random dialogue. It just goes a step further and asks for permission to publish the dialogue on profile wall. I have a test profile which is using this app and you can look at the wall: http://www.facebook.com/profile.php?id=100001044906526

Hello World in CakePHP – 3 steps

  1. Download and set up CakePHP in your webroot. Stop reading further if you cant do it or you are not sure how to do it.
  2. Create a file named “helloworld_controller.php” in folder “cakephp/app/controllers/helloworld_controller.php” and paste following code in the file
    <?php
    class HelloWorldController extends Controller {
    public $name = 'HelloWorld'; //Controller name, we need it.
    public $uses=null; //The example doesn't "use" any model.
    public $autoRender=false; //Do not render "automagically"
    public function index() {//The default action for a CakePHP controller
    echo __METHOD__;//This will print HelloWorld suffixed with some more info ;-)
    }
    }
  3. Point your browser to CakePHP installation on your server for example “http://localhost/cakephp/helloworld/”

Piece of cake… 🙂

Problem with NULL in MySQL

One fine morning you simply login to MySQL Terminal and…
[email protected]:~$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 61
Server version: 5.1.37-1ubuntu5.1 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select concat('Hello ',NULL,'World!');
+--------------------------------+
| concat('Hello ',NULL,'World!') |
+--------------------------------+
| NULL |
+--------------------------------+
1 row in set (0.00 sec)
mysql> select 1 + NULL + 3;
+--------------+
| 1 + NULL + 3 |
+--------------+
| NULL |
+--------------+
1 row in set (0.00 sec)
mysql>

WHAT THE ####???
This is unexpected. Normally one would expect concatenating a NULL with string will simply result in string. But MySQL gives you NULL. What???
Look here-> http://dev.mysql.com/doc/refman/5.0/en/problems-with-null.html.
No fear. We have a solution. :-). We will use IFNULL(). Have a look.
mysql> select concat('Hello ',IFNULL(NULL,''),'World!');
+-------------------------------------------+
| concat('Hello ',IFNULL(NULL,''),'World!') |
+-------------------------------------------+
| Hello World! |
+-------------------------------------------+
1 row in set (0.00 sec)
mysql> select 1 + IFNULL(NULL, 0) + 3;
+-------------------------+
| 1 + IFNULL(NULL, 0) + 3 |
+-------------------------+
| 4 |
+-------------------------+
1 row in set (0.00 sec)

One may also opt for COALESCE().