#native_company# #native_desc#
#native_cta#

Create a PHP-based Twitter Client with the PEAR Services_Twitter Package

By W. Jason Gilmore
on December 7, 2010

Like many developers, I tend to be pretty exacting when it comes to my programming environment. My disdain for any application that doesn’t work exactly as I expect it to extends well beyond development-specific tools, which was the primary motivation behind migrating my laptops to Ubuntu a year or so ago. The ability to tweak every conceivable aspect of the operating system — not to mention the broad selection of open source and commercial applications — has resulted in an enormous productivity boost and I’m immensely happy about making the switch.
One minor source of pain I’ve encountered with Ubuntu is in regards to the availability of Linux-based Twitter clients. I try to post several updates weekly, most of which point readers to my latest Developer.com and PHPBuilder articles (for updates follow me at @wjgilmore), and the existing clients so far have not fit my particular workflow. This isn’t to say there aren’t any quality clients; on the contrary there are many, including TweetDeck. However, as I’m the exacting sort, I wanted to create something that met a very specific set of requirements.
At the same time, entirely reinventing the wheel seems foolhardy, and so I wanted to base the project on a solid foundation, including a quality PHP-based Twitter library. That library turned out to be PEAR’s Services_Twitter package. In this article I’ll introduce you to this powerful package, which although still in beta already offers all of the features you’ll need whether you want to build your own Twitter client or simply add Twitter-specific functionality to an existing application. I’ll also talk about some of the finer points involved in creating a Twitter client that might be used in conjunction with multiple accounts.

Installing Services_Twitter

Like all PEAR packages, you can conveniently install Services_Twitter using the PEAR CLI. As I mentioned, Services_Twitter isn’t yet a production release, so you’ll need to pass the -f option in order to force installation. You should also pass the --alldeps option as Services_Twitter requires several other PEAR packages to work properly. In total, the installation command should look like this:

%>pear install --alldeps -f Services_Twitter-0.6.3

Registering Your Twitter Client

The Twitter API requires users to implement the OAuth authentication protocol, which is very convenient because it allows your client to be used in conjunction with multiple users without having to worry about internally managing their Twitter login details. It is, however, quite different from some of the more simplistic authentication solutions, so I suggest taking some time to peruse the Twitter API documentation if you’re not already familiar with OAuth.
Following that, you’ll need to register your Twitter application, which takes only a moment but you will need to define the URL that Twitter will return to after successful authentication (when building browser-based clients). After you’ve submitted the application you’ll be provided with a consumer key and a consumer_secret. Copy and paste this information into your IDE as we’ll be returning to it in a moment.