Welcome to my blog. If you like my posts, please subscribe to my feed and if you want to keep in touch, follow me on twitter! Happy reading!

Create Twitter Counter Badge using CodeIgniter

Wednesday, December 2nd, 2009

I know this is one of those “re-inventing the wheel’” kind of tutorials but I had nothing better to do during my Eid vacation :-P

Things you will need

1. Download and install the excellent CodeIgniter Curl Library

2. You will need an image which will contain your follower count.

The Code

In your Controller class add the following methods

function MyTwitterCounterBadge($username)
{
    $this->load->library('image_lib');
    $config['source_image'] = './images/YOUR_BUTTON_IMAGE.gif';
    $config['wm_text'] = $this->_getTwitterCounter($username);
    $config['wm_type'] = 'text';
    $config['wm_font_path'] = './system/fonts/arlrdbld.ttf';
    $config['wm_font_size'] = '12';
    $config['wm_font_color'] = 'ffffff';
    $config['wm_vrt_alignment'] = 'top';
    $config['wm_hor_alignment'] = 'center';
    $config['wm_hor_offset'] = '10';
    $config['wm_vrt_offset'] = '10';
    $config['dynamic_output'] = 'TRUE'; // DO NOT CHANGE

    $this->image_lib->initialize($config);

    $this->image_lib->watermark();
}

function _getTwitterCounter($username)
{
    $count = 0;
    $this->load->library('curl');

    $json = $this->curl->simple_get("http://twitter.com/users/show/$username.json");
    if( $json )
    {
        $obj = json_decode($json);
        $count = $obj -> {'followers_count'};
    }

    return $count;
}

 

Change the values of wm_font_path, wm_font_size, wm_font_color, wm_vrt_alignment, wm_hor_alignment, wm_hor_offset and wm_vrt_offset to fit your needs. Please refer to the CodeIgniter user guide to know more about these settings.

Now all you have to do is include an image tag in your view page that points to the controller function above

<img src="<?=BASE_URL?>CONTROLLER_NAME/MyTwitterCounterBadge/TWITTER_USER_NAME">

 

And that’s all there is to it. Now you have your own personalized twitter counter badge!

Happy Programming!

HOW TO: Display RSS Feeds in your PRADO app

Saturday, June 14th, 2008

This guide will show you how to easily parse and display RSS Feeds in your php application no matter what framework you use. However, I will be using the PRADO framework for this demonstration. The Parser Since I’m a big fan of open-source software, I used MagpieRSS. MagpieRSS provides an XML-based (expat) RSS parser in PHP. The toolkit can be very easily integrated to your PHP application with just one line of code. Follow these steps to install the toolkit into your prado application:

  1. Download the toolkit from here.
  2. Create a folder within the protected folder of your application. You can name it anything you like. I’ll name it Common. Now your applications directory structure should look something like this
  3. Extract the contents of the package that you downloaded in the previous step into the Common folder.
  4. Now you will see a file named rss_fetch.inc. Rename that file to rss_fetch.php. I’ll explain why I did this in the next step.
  5. Include the rss_fetch.php file in your prado class and call the parser within a function as follows
  6. Prado Directory Structure

If we don’t rename rss_fetch.inc to rss_fetch.php in the previous step we cannot use the prado style include statement as shown in Line 1; Instead we have to use include_once(..). I did that just to maintain consistency in my coding style – no biggie ;)