Thursday, May 1, 2008

So, yesterday I received an invitation to join Twitter.  I thought it was pretty neat, so I created an account.  Then I found you could add Twitter to your web page, and also discovered the Twitter API, so I could customize my implementation.  Works pretty good, but I did run into one bit of weirdness:  About 7:00 this morning, when I requested the list of updates from Twitter, my last post was dated May 1st at 12:15 AM, which was correct for my timezone.  About half an hour later, just as I was moving code to production, suddenly that date was showing up as 7:15 AM.  No change in the reported timezone.  So, I don't know quite what's up with that.  I adjusted for the date offset, but the fact that it suddenly changed concerns me a bit.  I'll have to keep an eye on things to see if those dates are really stable.  Is there a Twitter mirror or something that has the dates in a different time zone?  I don't get it.

As far as the implementation was concerned, I wound up using file_get_contents to fetch the data from Twitter into a string.  I then wrote my own parser to pull the data out of the XML document (my site is hosted on a server running PHP 4.7, so I couldn't use the latest XML tools).  This included a nice little function that returns the contents of a node based on the node name and starting position:

function getNodeContents($strXML,$nodeName,$startPos) {
	$nodeContents = "";
	$nodeStart = strpos($strXML,"<$nodeName>",$startPos);
	$nsEndPos = $nodeStart+strlen($nodeName)+2;
	if ($nodeStart !== false) {
		$nodeEnd = strpos($strXML,"</$nodeName>",$nsEndPos);
		if ($nodeEnd > $nsEndPos) {
			$nodeContents =  substr($strXML,$nsEndPos,$nodeEnd-$nsEndPos);
	return $nodeContents;

The function makes the simplifying assumption that the node has no attributes, which is true in this case.  I fetched the contents of each status node, grabbed the contents of the nodes that I wanted within that block, and placed them into an array.  Then, I used AJAX to make the call from my home page.  The AJAX was actually very simple.  The code is as follows:

<div id="twitter"></div>
<script language="JavaScript">
var xmlDoc = null;

function getTwitter() {
	xmlDoc = new XMLHttpRequest();
	xmlDoc.onreadystatechange = loadTwitterData; "GET", "twitter.php", true );
	xmlDoc.send( null );

function loadTwitterData() {
	if ( xmlDoc.readyState != 4 ) return;
	document.getElementById("twitter").innerHTML = xmlDoc.responseText;


My local Twitter script did all the processing and formatting of the data, then the javascript just drops the result into my home page.

Next item on the agenda is to write a script that will let me post updates to Twitter from my website.

Leave a comment

The church needs to do more about the injustice of abortion than merely shake their heads and say, “What a shame.”