This blog - now archived - is read-only.
New blog: Mohammad Emran Hasan.

Category Archives: Web Development

Painless Ruby and Rails installation on Ubuntu/Mint

It took me an hour to properly install the latest versions of ruby, rails and its mysql binder. Although its less than a 5 mins task, I had to search in forums, blogs to deal with the various errors. So just posting this so its easy for someone else to get done with this in 5 mins.

1. Open Terminal and install RVM (Ruby Version Manager)

bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer

2. Enter the following in ~/.bash_profile or in ~/.bashrc (replace YOUR USER NAME with the username you’re logged in with):

[[ -s "/home/YOUR USER NAME/.rvm/scripts/rvm" ]] && source "/home/YOUR USER NAME/.rvm/scripts/rvm"  # This loads RVM into a shell session.

3. Install Ruby 1.9.2

rvm install 1.9.2

Continue reading

Quick start on new Facebook PHP SDK (IFrame based)

This post is more than couple year old. It solved the issues when the SDK was first released and facebook had lack of documentation. However, now facebook has very rich set of documnetation and you should follow that instead!

The new Facebook API has already spread over the application developers and if you’re like me, you’ve already got your hands dirty to see how this new thing works. If you have tried to follow the documentation to authorize/get session in your canvas application, it is likely you have already hit roadblocks. Well, I am no savior but I have glued together a few clues and got it working for myself.

I am assuming that you have already created your application by following the Getting Started section from the official documentation. Also, this is for IFrame based applications only.

Enough talking, let’s get some code.

Step 1: Get the new SDK

Download the new SDK from github. We will only need the facebook.php file from the src folder. In our project directory, let’s create a folder called “lib” and put the file there.

Step 2: A configuration file

Let’s now create a configuration file to store our facebook configuration. Let’s name it config.php. Here goes the source:

<?php

define("FACEBOOK_APP_ID", '113795715321151');
define("FACEBOOK_API_KEY", '064baf5fb98de050cd7b9a001ca1988b');
define("FACEBOOK_SECRET_KEY", '430f43c01f6dfe02c284b4545976f9ce');
define("FACEBOOK_CANVAS_URL", 'http://apps.facebook.com/emran-test-app/');

Step 3: Application Main Page

This file will be the main entry point to our facebook application. It just instantiates the facebook object, sets the configuration and checks for a valid session. If it does not find a valid session, it redirects to the login page. For first time visitors, it will be the authorization page. On later requests, the operation will occur in the background – without any user interaction.

<?php

include_once 'lib/facebook.php';
include_once 'config.php';

$facebook = new Facebook(array(
    'appId'  => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_SECRET_KEY,
    'cookie' => true,
    'domain' => 'phpfour.com'
));

$session = $facebook->getSession();

if (!$session) {
    
    $url = $facebook->getLoginUrl(array(
        'canvas' => 1,
        'fbconnect' => 0
    ));

    echo "<script" . " type='text/javascript'" . ">top.location.href = '$url';</script>";

} else {

    try {

        $uid = $facebook->getUser();
        $me = $facebook->api('/me');

        $updated = date("l, F j, Y", strtotime($me['updated_time']));

        echo "Hello " . $me['name'] . "<br />";
        echo "You last updated your profile on " . $updated;

    } catch (FacebookApiException $e) {

        echo "Error:" . print_r($e, true);

    }
}

Continue reading

Enterprise PHP

I presented this talk on the Soft Expo 2010 – the largest software fair in Bangladesh. The intention was to clear some of the misconception about PHP, the growth of PHP, how it can fit in the enterprise now, etc.

After these, I shed light on some topics that a company/developer should keep in mind in order to write good software in PHP. This was followed by live session on caching, mysql query optimization, use of Xdebug, etc.

So here goes the presentation:

And a big thanks to Ivo Jansch’s “PHP in the real wolrd” presentation, from where I took inspiration.

Cheers!

Changing the default controller naming convention in CodeIgniter

CodeIgniter is one of my favorite framework and I often use it for developing application quickly. Although it is very flexible in most cases, I find its naming convention to be strict. Many times I have faced this problem when my controller’s class name and a model/library’s class names are the same – a Fatal error is inevitable and I have to forcefully change the name of the conflicting class to something less desirable (say from Users to UsersModel). Today I wanted to end this problem.

So I extended the CI_Router core class and made change to the _validate_request method. Now I can name my controller classes in this fashion: UsersController and it resides on the file system as controllers/UsersController.php. If you’ve tried other established frameworks, you should notice that this naming convention is widely used. So, if you have the same need, then just download the MY_Router.php file and put it on your application/libraries folder. That’s it.

Here is how your controller would start:

[cc lang='php']
< ?php

class UsersController extends Controller
{
public function __construct()
{
parent::__construct();
}
}
[/cc]
Continue reading

Facebook Developer Garage Dhaka – Open Stream API

The first ever “Facebook Developer Garage” took place in Dhaka, Bangladesh and I am proud to be a part of it. I was one of the speakers of this spectacular event organized by Facebook and IBT bangladesh. I spoke about the recently released Open Stream API from Facebook. It was more of an overview rather than a detail one. Here goes the slides:

I have created a quick start example which outlines both Facebook Connect and the Open Stream API. You can check that here. A blog post covering the steps is coming soon in 3-4 days.

Stay tuned if you have interest in them.

Cheers

phpXperts ’09 seminar

At last the phpXperts seminar on “Current Web Trends” have taken place and I was one of the speakers there. Among all the topics, mine was a bit suggestive and naturally a bit less exciting. I spoke about how to become a PHP ninja – what are the characteristics they posses, what are the tools they use, what are the paths they follow etc. It’s actually a follow-up of one of my blog posts: “Becoming a kick-ass PHP ninja“.

Here goes the slides:

All the other slides, pictures, and videos are available here.

Enjoy!

PHP Payment Library for Paypal, Authorize.net and 2Checkout

Update: This library has gone through full re-write and the uploaded version can be found here.

If you are like me, whenever you need to work with a 3rd party API or a gateway, you’d first search in Google for a possible wrapper for it in PHP. When it comes to supporting payment gateways, you get bunch of libraries in the search results who are fundamentally different. Some of them are old PHP 3/4 ones, some are new, some may need PEAR, etc.

As they were not required together in one single project, I used them whenever needed. But in one project, I needed them all. I thoughts it’s a chance and decided to stop using them and wrote my own ones where I can use the same methods for all the gateways.

So, here is an abstract PaymentGateway library which is being extended to be used for three popular payment gateways (Paypal, Authorize.net, and 2Checkout) in order to provide you with a similar way of using them. Note that the libraries are for basic usage only and do not contain options for recurring payments. Without much babble, let’s see a few examples of how you can use them.

Download
No download available for this version.

Paypal

In order to process payments using Paypal, you’ll need to follow these steps:

1. Send the required information to Paypal (snippet 1). Be sure to specify your Paypal email where you want to receive the funds, the success and failure pages, the IPN page, and the product information. The example has the test mode ON, which you will not need in real scenario.

2. Create a payment success page where Paypal will send your customer after payment.

3. Create a payment failure page where Paypal will send your customer after failed payment.

4. Create a IPN page where Paypal will send payment notification in the background. Make sure you use/remove the test mode in conjunction with step 1. (snippet 2)

Snippet 1


// Include the paypal library
include_once ('Paypal.php');

// Create an instance of the paypal library
$myPaypal = new Paypal();

// Specify your paypal email
$myPaypal->addField('business', 'YOUR_PAYPAL_EMAIL');

// Specify the currency
$myPaypal->addField('currency_code', 'USD');

// Specify the url where paypal will send the user on success/failure
$myPaypal->addField('return', 'http://YOUR_HOST/payment/paypal_success.php');
$myPaypal->addField('cancel_return', 'http://YOUR_HOST/payment/paypal_failure.php');

// Specify the url where paypal will send the IPN
$myPaypal->addField('notify_url', 'http://YOUR_HOST/payment/paypal_ipn.php');

// Specify the product information
$myPaypal->addField('item_name', 'T-Shirt');
$myPaypal->addField('amount', '9.99');
$myPaypal->addField('item_number', '001');

// Specify any custom value
$myPaypal->addField('custom', 'muri-khao');

// Enable test mode if needed
$myPaypal->enableTestMode();

// Let's start the train!
$myPaypal->submitPayment();

Continue reading

Code Updates (HTTP class, Extended CodeIgniter Model, Cross-domain AJAX transport)

Greetings to all the readers of my blog.

Many of you have been writing to me in the last couple days when I took the site down. The main objective was to add the new theme and push a few code updates of the existing libraries. I really appreciate your concern and would like to reassure you that the site is up and will be up as usual :) Now, besides the slightly customized theme from Elegant Themes, I have put a few code updates. They are detailed below:

Extended Model for CodeIgniter

The original version of the Extended Model for CodeIgniter has been serving many people well. Although most users loved the nifty functions of the Model, many (including me) didn’t like the hacking of CI core to get this functionality. With the release of CodeIgniter 1.7, we can avoid that as we can now overload the Model class of CI like the other libraries. Follow this instruction:

1. Download the updated version from here
2. Put it in your application/libraries folder
3. In your model files, use it this way: class Product extends MY_Model
4. Everything else is same just like the original one

HTTP Class

There is not much update in this class except for a few bug fixes (thanks to you guys). Also, I have included a license file in the package as many of you have asked. It’s released under the MIT license. The original post is here for reference.

Download the update from here and the API reference is here.

Cross-domain AJAX calls using PHP

A minor bug fix in the code. Thanks to a few of you who pointed them out. Original post is here. Download the update from here.

I have accelerated plans for the blog in 2009 so stay tuned for some worthy posts in this month. And do write to me if you feel you have any questions/ideas/suggestions.

Cheers!