Dimitry’s Blog

Twitter status on Virb or MySpace

April 3rd, 2007

Twitter on VirbFirst, let me begin by apologizing for putting Virb and MySpace in one sentence. That won’t happen again.

Last week, I came up with a quick solution to include my Twitter status on my Virb profile. This way, all my info is consolidated nicely within one profile. For now, Virb makes this a bit hard because Javascript cannot be included in profiles and rightfully so!

So, to get around that, I simply decided to have my web server query my Twitter status from time to time (every 10-15 minutes) and if there’s a change, create a new image accordingly. The image is then simply plugged into my Virb profile and loaded from my server upon every viewing.

Requirements: PHP 5 (or PHP 4 with a custom JSON module) with both GD library and FreeType compiled with it. Most hosting companies will have both available. If not, ask them. You may not even need FreeType if you decide to use one of few built in fonts (ugly). FreeType is needed to be able to use custom fonts when generating custom images using the GD library.

Step 1 - Get the data

We first need to get the data from Twitter and they make it easy enough with two options: JSON and XML. Being a Javascript developer, I swear by JSON and since PHP 5 has all the necessary functionality built in to parse a JSON string, let’s use that.

$url = 'http://twitter.com/statuses/user_timeline/771362.json'; // Get the JSON URL to your account

$curl_handle = curl_init(); // Open up a CURL connection
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); // Timeout connecting in 2 seconds
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 20); // Timeout getting data in 20 seconds (since Twitter has been slow with growing popularity)
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); // Get data back

$buffer = curl_exec($curl_handle); // Execute the request and get data back
curl_close($curl_handle); // Close the connection!

if ($buffer) { // If data is returned
	$data = json_decode($buffer); // Decode JSON string

	$twitter_when = date('n/j/Y g:ia',abs(strtotime($data[0]->created_at . " GMT"))); // When was last tweet posted?
	$twitter_text = $data[0]->text; // Last tweet
} else { // Otherwise
	die("Couldn't fetch data"); // Error occurred while fetching, most likely a timeout
}

We now have the time of the last tweet and the actual tweet in variables $twitter_when (UNIX timestamp) and $twitter_text respectively.

Note: You need to update $url variable to have your userid (mine is ‘771362′). It’s tricky to find it out on Twitter. I will post an update when I figure out a good way to do so.

Step 2 - Generate the image

We need a way to display this new data on Virb and since no Javascript is allowed, we will generate a new image with this content and then include the image in our Virb profiles.

//$im = imagecreatetruecolor(560, 72); // Uncomment this and delete the next line to create a blank image
$im = imagecreatefrompng("virb.png"); // This creates an image from an existing PNG image

$white = imagecolorallocate($im, 173, 173, 173); // Define some colors to use
$grey = imagecolorallocate($im, 100, 100, 100); // Color for side the date/time

$twitter_text = wordwrap($twitter_text,72); // Because my image is only 560 pixels wide, text can run out of the boundaries and GD library has nothing to remedy that, so this function will wrap the text after 72 characters. This may differ for your image.

$font = 'SomeFont.otf'; // Custom font. You may use one of four or five standard fonts, but they're ugly

imagefttext($im, 10, 0, 10, 20, $white, $font, $twitter_text, array('linespacing'=>1.7)); // Write out to the image Twitter status with appropriate positioning
imagefttext($im, 9, 0, 11, 61, $grey, $font, $twitter_when, array('linespacing'=>1.7)); // Same with date/time of last tweet

imagepng($im,"twitter.png"); // Create twitter.png
imagedestroy($im); // Free up some memory

We now have an image that was generated on the fly by PHP that includes our last tweet. This image can now be safely included anywhere you’d like, even your website.

I didn’t go into describing the functionality of each function and its parameters, so make sure you look them up on php.net for better reference.

Step 3 - Run the final script recurrently

Instead of refreshing the PHP file yourself every time you update your Twitter status, simply setup a cron job on your web server. I have mine setup to run the PHP script every 10 minutes.

Also, to save some server resources and not update the image if Twitter status hasn’t changed, I also modified the code a bit to save the latest tweet in a text file. If upon PHP file execution Twitter status hasn’t changed, it won’t waste resources and generate a new image, but rather simply stops the execution.

My cron job looks like this (the location of PHP may be different on your web server):

/usr/bin/php5 -q /home/10416/domains/arthero.com/html/twitter_update.php

Download the final file below (and rename to .php):
» Twitter status into image

Look at the final result:
» Final image included in Virb
» Template image used to create it

Note: The code won’t work right away unless you specify a custom font and change the function to use default font!

  1. Brad

    April 3rd, 2007 »

    Awesome work man! Kudos. :)

  2. Richard Crowley

    April 3rd, 2007 »

    Cool hack, Dimitry. Your code samples got me thinking, though. Why does PHP make you configure your cURL session each and every time. So I got to writing: http://richarddcrowley.org/blog/view/125

    I haven’t gotten into the Twitter thing yet (which is hard having gone to SXSW), but its still awesome to see MySpace bent to do something it doesn’t want you to do. (I may have it out for MySpace.)

  3. Richard Crowley

    April 3rd, 2007 »

    Also, my use of the em tag seems to have broken your CSS, could you remove the tag and just leave the word “may” in place?

  4. Dimitry

    April 3rd, 2007 »

    @Richard: Fixed the em tag issue! Good eye. Viewing your post now

  5. Rob Schultz

    April 4th, 2007 »

    I’m always impressed by your ideas. Despite the fact that I don’t have any of the 3 (Virb, Myspace, & Twitter) I may now have to get 2 of them (die Myspace ;) ) just to try it out!

  6. Astorg

    April 29th, 2007 »

    Cool one! I have to say…

  7. Anand

    May 6th, 2007 »

    Once I get a phone I’ll get twitter..

    And once I go to college or launch April Zero, I’ll build a pimped out myspace/virb to match.

    That dynamic text image creation is sweet stuff, I’ll have to try that out sometime!

  8. Jamison F.

    September 6th, 2007 »

    You can get your JSON ID by looking at the url for your RSS feed.


cialis overnight buy lasix online order levitra buy no rx cialis buy cheapest viagra online accutane discount find cialis on internet purchase synthroid find cialis lasix online cheap buy cheap lasix approved viagra pharmacy viagra for order buy synthroid online zithromax generic buy cheap viagra cialis sale cheap levitra clomid generic synthroid for sale accutane online cheap cheap viagra in uk cheap acomplia tablets cheap viagra without prescription where to buy soma order zithromax buy generic viagra online cheap acomplia levitra online purchase viagra without prescription order propecia cheap price cialis where to buy clomid accutane without prescription cialis medication propecia cost viagra best price cialis online acomplia cheap soma online order soma buy lasix cheap discount cialis propecia without prescription cheapest clomid buy cheap zithromax price of soma acomplia generic clomid pills viagra pills order viagra on internet zithromax prices discount viagra without prescription cialis malaysia buy cialis on internet zithromax without prescription discount zithromax cialis tablet lowest price for cialis viagra cheap cialis cheap drug cheap cialis overnight delivery buy cialis cheap propecia cheap zithromax online stores viagra no rx required lasix online stores buy propecia cheap order synthroid generic cialis online viagra in malaysia cheap cialis in usa buy cheap soma