Ashley Sheridan​.co.uk

Using cURL to Interface with Twitter

Posted on

Tags:

Twitter. Love it or hate it, it's here to stay for a while. One recent project I worked on required some integration between Twitter and the website in I was working on. Having only a short while to work on this, and not being familiar with any of the various plugins out there, I wrote a simple function using cURL. While this example will focus on grabbing the number of followers of MicroMart magazine (the most popular weekly computer magazine in the UK) it will still show general cURL techniques that you can employ on any such site.

function followerCount() { $url = "http://twitter.com/followers/ids.xml?screen_name=micromart"; $c = curl_init(); curl_setopt($c, CURLOPT_URL, $url); curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); $contents = curl_exec($c); curl_close($c); $lines = explode("\n", $contents); $followers = 0; foreach($lines as $key => $line) { $pattern = "/^/"; if(preg_match($pattern, trim($line))) { $followers++; } } print $followers; }

This is not a perfect example, but at the time of writing, the server didn't have access to any DOM capabilities, so I had to make do with an ugly loop and some regular expression wizadry.

Line 3 creates the URL that you will be calling. If you copy and paste this into your browser window, you will see that rather than an HTML page, you actually get an XML document returned instead. It is this document that we want to work with.

Lines 5 - 7 set up some default options to use in this cURL transaction. Of particular note is line 6, which basically tells the remote server that your cURL request is actually coming from the Firefox 2 browser, and not a PHP script at all. This is necessary for some web services, like the WoW Armory for example, as sometimes the server looks to see if the browser making the request is capable of handling the returned information, otherwise it might send something you don't expect. To make use of the WoW example; their Armory site runs on an XML framework, which is converted into HTML by the browser. If the server recognises that the browser making the request does not have the capabilities to do this, it returns a simpler HTML page.

In line 9, I grab the entire XML contents from the server, and then close the connection in line 10. By closing the connection, your web server can then free up some memory if it wishes that it was using to keep the connection open.

The script breaks up the contents into an array of lines, and then loops though the lines, one by one comparing each to a regular expression. The regex just looks for lines that start with <id>, as each one of these lines indicates a user id number in Twitter. Once the loop is complete, the resulting number of followers held in $followers is output.

This is a very simple example, but that's good to start with. As the XML gets more complex, you really should look at DOM functions, as they can make your job of extracting data much easier, and gives you much more freedom if the specification of the XML changes slightly.

Comments

Leave a comment