Home arrow PHP arrow Page 4 - Dynamic Twitter Signature Images with PHP

Grabbing the avatar image - PHP

Twitter is the latest and greatest craze in social networking sites. As such, many developers and businesses are using it as a marketing technique. Today we’re going to come up with a method for creating a dynamic signature image for forum posts and emails that will automatically load your Twitter feed and display your latest Twitter status. We’ll be using PHP to grab the content along with GD2 support to create the image.

TABLE OF CONTENTS:
  1. Dynamic Twitter Signature Images with PHP
  2. Building the class
  3. Parsing the XML feed
  4. Grabbing the avatar image
By: Nilpo
Rating: starstarstarstarstar / 3
August 11, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

One of the details we were able to parse from the XML feed was the location of the user’s Twitter avatar.  We’re going to display this on our signature image, so we’ll need to make another CURL request to grab the image and store a copy of it locally.

    private function fetchAvatar($url, $adir)

    {

        $fname      = end(explode('/', $url));

        $adir       = preg_match('#^(.*?)/$#i', $url) ? $adir : "{$adir}/";

        $fname      = $adir . $fname;

        if ( !file_exists($fname) ) {

            $img = $this->curlRequest($url);

            $fp = fopen($fname, 'w');

            fwrite($fp, $img);

            fclose($fp);

        }

        $this->local_avatar = $fname;

    }

This function is pretty basic.  It uses the curlRequest function to retrieve the avatar image and write it to a local file.  You’ll see a couple of really nifty PHP techniques employed in the first two lines.

The first technique is a quick method of parsing the image file name from a URL.  You’ll recall that URLs have several parts, all delimited by a forward slash.  PHP’s explode function can create an array from a string based on a string delimiter.  This very quickly creates an array of each of the URL parts.  The end function then returns the last element in the array (being the file name portion of the URL).  This is a quick and dirty technique for returning the end portion of a delimited string.

The second technique combines a regular expression match with the ternary operator.  It checks the avatar directory string for a trailing slash and returns either the path if it includes one or the path with a slash appended to it.  This nice one-liner ensures that our path always contains a trailing slash.

Recapping 

Our Twitter SignatureImage class is nearing completion.  We’ve added all of the functions to retrieve a user’s Twitter feed information and copy the user’s avatar image.  In the second part of this article series we’ll take a look at building and rendering the actual signature image.

We’re also going to look at adding appropriate error handling and a file caching feature.  The latter is important since Twitter limits the number of API requests a single IP may request in a specific amount of time.  Caching will store a copy of the XML feed locally to prevent making too many requests to the API service. 

I’ll also be demonstrating a quick method of tracking how many impressions your image gets which is useful if you’re using your image in forum signature.  And of course, I’ll show you how to actually implement the SignatureImage class.  Until next time, keep coding!



 
 
>>> 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: