PHP
  Home arrow PHP arrow Caching Dynamic Twitter Signature Images with PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Caching Dynamic Twitter Signature Images with PHP
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2009-09-01


    Table of Contents:
  • Caching Dynamic Twitter Signature Images with PHP
  • Implementing caching
  • Understanding the logic
  • Finishing up the application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Caching Dynamic Twitter Signature Images with PHP
    ( Page 1 of 4 )

    Welcome to fifth and final part of this series on creating a dynamic Twitter signature image in PHP. In the last segment, I showed you how to implement custom PHP exceptions as an error-handling mechanism in your signature image application. Today we’re going to wrap up our signature image application by adding a caching feature. This is a two-fold solution that both boosts performance and overcomes a pitfall in the Twitter API.

    As a result of heavy usage, Twitter has begun limiting the number of API requests that it will serve to a single IP address within a given amount of time.  This limitation is generally around 50 requests per hour.  While this is usually not a problem, if you begin using your signature image on high-traffic sites such as some forums, you may find that Twitter will start cutting off your requests.  At first that may seem confusing, but keep in mind that regardless of where the image actually appears, the request always originates from the IP address of the web server where the script is located.

    To circumvent this pitfall, we can add a caching feature to our Twitter signature image application.  Accomplishing this is a fairly straightforward process.  The Twitter user feed will be saved in a cache and served locally, and the cache will be updated at intervals, drastically reducing the number of direct requests to the Twitter API.  Since the Twitter feed is a small XML-formatted text file, the cache does not require much space and the local server load is nearly negligible.

    Setting up caching

    To begin caching we'll need to add another folder in the directory where our Twitter script resides.  This folder will be used for storing the cached user feeds.  You can name it anything you like, but for simplicity I just named it "cache."

    class SignatureImage

    {

        private $screen_name;

        private $profile_image;

        private $status_text;

        private $local_avatar;

        private $cache_expires = 900;

    The next step involves making updates to the SignatureImage class.  The first of these changes will be to add a new properly called $cache_expires.  This variable holds the value that tells the script how often the cache should be updated.  This value, in seconds, determines how often the script will allow a new request to be made to the Twitter API. 

    A value of 900 seconds (or 15 minutes) is a good place to start.  Most Twitter feeds are not going to be updated more frequently than 15 minutes, so there's no need to fetch the feed more often than that.  You can increase or decrease this number as necessary based on the performance of your own script and the number of different users actively using it.  More users may require a longer wait time while less users may require less.  Using a value of 900 seconds, the script can support up to 12 users making 4 requests per hour.

        public function __construct($name, $bg_image, $adir, $cdir)

        {

            try {

                if (!$name) {

                    throw new SignatureImageException('You must provide a user name.');

                }

                $this->fetchUserInfo(strtolower($name), $cdir);

                $this->fetchAvatar($this->profile_image, $adir);

                $this->renderImage($bg_image);

            }

            catch (SignatureImageException $e) {

                $e->getImage();

            }

            catch (Exception $e) {

                die($e->getMessage());

            }

        }

    Next, you'll need to update the SignatureImage constructor to allow an additional parameter for specifying the location of the cache directory.  I've named this parameter $cdir.  I've also passed this value to the fetchUserInfo method call.  We'll be adding this change later. 

    Finally, I've added the use of PHP's strtolower() function to ensure that the user name is always passed to the fetchUserInfo method in lowercase form.  This will prevent caching duplicate user feeds, as you'll see later.

    new SignatureImage($name, 'banners/my_banner.jpg', 'avatars', 'cache');

    Once you've made all of these changes to the SignatureImage class constructor, you'll need to update your source call to the SignatureImage class to include the added parameter.



     
     
    >>> More PHP Articles          >>> More By Nilpo
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek