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><a href="http://wefollow.com">WeFollow</a></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/ <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 & Canada)</time_zone> <profile_background_image_url>http://s3.amazonaws.com/ <profile_background_tile>false</profile_background_tile> <statuses_count>11</statuses_count> <notifications></notifications> <following></following> </user> </status> </statuses>
blog comments powered by Disqus |
|
|
|
|
|
|
|