Basic API Integration: Twitter
Mar 25, 2009 by David Brooks
When I started on the design for this site I wanted to integrate my Twitter content here as well. As with anything external, there are a few different options on how you can bring it into context. Being a web standards guy I wanted everything to be completely integrated with plain text and non-externally hosted code.
I know from experience that one of the issues with integrating external information is the increase in load times. Any time you have to query another server it’s going to take longer. Typically the solution is to put your external code, badge, whatever, after your content. Then, as the item loads it doesn’t drag down the rest of everything.
But that wasn’t good enough for me.
I wanted it to load just as quickly as the rest of my site, and to do that I needed to host the most recent data locally.
cURL
cURL is an open source project that interacts with an external URL. If I’m not mistaken it’s often included in hosting packages, and is more or less the standard way of retrieving a foreign asset. I think the cURL website does a great job of explaining what cURL does:
curl is a command line tool for transferring files with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS and FILE. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos…), file transfer resume, proxy tunneling and a busload of other useful tricks. – The cURL Site
So, if cURL can safely connect to an external resource and bring that information over to me, why not use it? Here are the commands I needed to issue from my PHP script in order to get my Twitter feed as an XML document; your exact commands may vary.
$urlRequest = @curl_init('http://twitter.com/statuses/user_timeline/username.atom');
$file = fopen("/entire/link/twitter.xml", "w");
curl_setopt($urlRequest, CURLOPT_FILE, $file);
curl_setopt($urlRequest, CURLOPT_HEADER, 0);
curl_exec($urlRequest);
curl_close($urlRequest);
fclose($file);
Now, in there you will need to replace at least two pieces of information. The first one is your Twitter name (username.atom). The second thing to replace is the directory in which you would like to save the file (/entire/link/).
The commands above will go out to the requested URL and grab the atom file. Then, it will open the file we specified and dump the contents of our feed into a local version of the file ($file). Once everything is completed it closes the file.
Parsing the XML
Now that we have our information as a XML file we can start to interact with the data. If you go to the URL that you just requested it will show you what the XML document should look like. You should also open up your duplicate copy of the XML file to make sure everything looks good, and is in fact a reasonable representation of your Twitter feed.
If your information matches, you just need to start converting the XML into XHTML or HTML. The code I used looks like this:
$rss = simplexml_load_file('/entire/link/twitter.xml');
echo "<ul>";
$x = 0;
// Here we'll put a loop to include each item's title and description
foreach ($rss->entry as $item) {
$said = $item->title;
$said = ltrim($said, "luzcannon: ");
if (!eregi("@", $said)) {
if ($x < 2) {
echo '<li><a href="' . $item->link[href]. '">' . $said . '</a></li>';
$x++;
}
}
}
if ($x == 0) {
echo '<li>Unfortunately Twitter is unreachable for comment. Feel free to <a href="http://www.twitter.com/luzcannon" rel="me">follow me</a> for updated messages.</li>';
}
echo "</ul>";
The PHP code above will roll through the XML document (referenced in the $rss variable), declaring each thing I said as the variable $said. It then trims out “luzcannon:”, because I don’t really need to show that I said it on my own site. And then, finally, it starts the next loop that determines if what I said is directed at another user or not.
To me, this was important because I didn’t think most people visiting my site would care about what I said to other users. If you want to include that, just remove the loop and everything should work correctly. In the case of this example it should create an unordered list two items long. (Increase the number of displayed items by changing $x < 2 to whatever number you want.)
Error Correcting
If your feed is empty, maybe because of a Twitter load time error, it just displays a message that Twitter is down, and that the user could just follow you for your messages instead.
There is definitely room for more error checking here, but it was sufficient for my purposes.
Automation
Obviously we don’t want to have to continue to update this manually, so we’re going to need a cron job to keep things current. You’ll need to check with your web host’s documentation on how to implement cron jobs on their system.
However, I found out the hard way that Twitter only likes to be queried every so often. If you have something like Twitteriffic, it’s already making requests, so your script may not be able to retrieve information if its fighting for the same number of data requests. In order to keep the “Twitter is down” error from showing on my site I changed how often the cron job was running. Currently I have mine set to update every 15 minutes. That should keep things below Twitter’s limit as well as reasonably up to date.
Along the same lines, you also don’t want to request the information from Twitter every time someone needs to see what the latest messages are. So, you’ll want to have an update script that contains all of the cURL stuff and a display script that parses out the content. You’ll want to include the display script on your page while the update script should only be accessed through the cron job.
But, if you keep your requests down everything should work properly and your readers should see the latest information from your Twitter feed.






Post new comment