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

Building the class - 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
 

With all of the basics out of the way, it’s time to dive into some PHP and begin building a class.  We’re going to create a class called TwitterSignature.  This class will load the background and user feed, create the signature image, and then output it to the browser as an image.  This will allow us to simply point the HTML source of an image to this PHP file and have it render in any page, as you’ll see later.

class SignatureImage

{

    private $screen_name;

    private $profile_image;

    private $status_text;

    private $local_avatar;   

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

    {

    }

}

Here’s the basic structure of the entire class.  We first create a few class-level variables that we will use as we start adding functions.  $screen_name, $profile_image, $status_text, and $local_avatar are all strings that will contain the Twitter screen name, URL to the Twitter avatar, the XML status feed, and local avatar directory path, respectively.  I’ll explain each of these in more detail as we go along. 

Notice also that I’m using a constructor for this class.  All of the class's functions will be called internally through this constructor.  There won’t be any need to call functions when the class is actually implemented.

The constructor accepts three required parameters: the Twitter screen name to process, the location of the background image, and the local directory where avatars are/will be stored.  The Twitter screen name is the most important, as we’ll need that to access the Twitter feed.

Twitter provides an API with which you can access a user’s status feed in XML format.  This is available from a public access URL in the form of:

http://twitter.com/statuses/user_timeline/screenname.xml?count=1

Replace screenname with the user’s Twitter screen name.  Adding the query string “count=1” forces the API to return only the latest status message, slimming down the request a bit.  Since we’re only interested in the latest status, it shortens both the response time and the request length, which increases the performance of our script.  A user feed looks something like this:

<?xml version="1.0" encoding="UTF-8"?>

<statuses type="array">

<status>

  <created_at>Thu Apr 16 04:48:43 +0000 2009</created_at>

  <id>1531458683</id>

  <text>Just added myself to the http://wefollow.com twitter directory under:  #technology #windows #guru</text>

  <source>&lt;a href=&quot;http://wefollow.com&quot;&gt;WeFollow&lt;/a&gt;</source>

  <truncated>false</truncated>

  <in_reply_to_status_id></in_reply_to_status_id>

  <in_reply_to_user_id></in_reply_to_user_id>

  <favorited>false</favorited>

  <in_reply_to_screen_name></in_reply_to_screen_name>

  <user>

    <id>25939902</id>

    <name>Nilpo</name>

    <screen_name>WindowsGuru</screen_name>

    <location>Salem, Ohio</location>

    <description>Ask the Windows Guru!</description>

    <profile_image_url>http://s3.amazonaws.com/twitter_production/
profile_images/106885592/vista_logo_3__normal.jpg</profile_image_url>

    <url>http://www.nilpo.com</url>

    <protected>false</protected>

    <followers_count>15</followers_count>

    <profile_background_color>9AE4E8</profile_background_color>

    <profile_text_color>333333</profile_text_color>

    <profile_link_color>0084B4</profile_link_color>

    <profile_sidebar_fill_color>DDFFCC</profile_sidebar_fill_color>

    <profile_sidebar_border_color>BDDCAD</profile_sidebar_border_color>

    <friends_count>9</friends_count>

    <created_at>Mon Mar 23 02:56:34 +0000 2009</created_at>

    <favourites_count>0</favourites_count>

    <utc_offset>-18000</utc_offset>

    <time_zone>Eastern Time (US &amp; Canada)</time_zone>

    <profile_background_image_url>http://s3.amazonaws.com/
twitter_production/profile_background_images/8921066/
3237101143_78dbb23e39_o.jpg</profile_background_image_url>

    <profile_background_tile>false</profile_background_tile>

    <statuses_count>11</statuses_count>

    <notifications></notifications>

    <following></following>

  </user>

</status>

</statuses>



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