Home arrow PHP arrow Caching Dynamic Twitter Signature Images with PHP

Caching Dynamic Twitter Signature Images with PHP

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.

TABLE OF CONTENTS:
  1. Caching Dynamic Twitter Signature Images with PHP
  2. Implementing caching
  3. Understanding the logic
  4. Finishing up the application
By: Nilpo
Rating: starstarstarstarstar / 1
September 01, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: