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 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.

Logging into the Twitter Account

When building browser-based Twitter clients, the OAuth authentication procedure is a two-step process that requires the user to visit the Twitter website at least once in order to confirm that he would like to provide the application with access to his account details (see Figure 1).

PEAR Services_Twitter Package
Figure 1. Confirming Access to My Tweephp Application
The code used to carry out this authentication process is undeniably long, made even more so by the fact that I didn't want to potentially confuse anybody by abstracting away some of the details. I suggest taking a few moments to review the following code, keeping in mind that the OAuth authentication process is accomplished in two steps. The first step (defined by the first part of the if conditional) sends the user to Twitter to confirm his wish to grant the application access to his account. The second step (defined in the second part of the if conditional) retrieves the OAuth access token following successful authentication, allowing the application to begin interacting with the user's account.


  require_once 'Services/Twitter.php';
  require_once 'HTTP/OAuth/Consumer.php';

  define('CONSUMER_KEY', 'SECRET');

  try {

    $twitter = new Services_Twitter();

    // If the user session isn't active
    if (! isset($_SESSION['token']))

      $oauth = new HTTP_OAuth_Consumer(CONSUMER_KEY, CONSUMER_SECRET);

      $_SESSION['token']        = $oauth->getToken();
      $_SESSION['token_secret'] = $oauth->getTokenSecret();
      $url = $oauth->getAuthorizeUrl('');

      header('Location: '.$url);

    // Authentication successful, retrieve the access token
    } else {

      $oauth = new HTTP_OAuth_Consumer(CONSUMER_KEY, CONSUMER_SECRET, 
          $_SESSION['token'], $_SESSION['token_secret']);

      if (isset($_GET['oauth_identifier'])) {


        $_SESSION['token']        = $oauth->getToken();
        $_SESSION['token_secret'] = $oauth->getTokenSecret();




  } catch (Services_Twitter_Exception $e) {

    echo $e->getMessage();


Keep in mind that the user has to travel to Twitter only once, as not only will a session be active following successful authentication, but Twitter also will know which applications the user has granted access to.

Displaying a Twitter User's Recent Messages

When authenticated, the application is free to retrieve messages, submit updates, and carry out other tasks on the user's behalf. For instance, the following snippet will output a list of all of the user's recent updates:
$response = $twitter->statuses->user_timeline();

foreach($response AS $item)
  printf("<p>On %s %s tweeted:<br />%s</p>", 
    $item->created_at, $item->user->screen_name, $item->text);

Submitting Updates

To submit an update, just use the update() method as demonstrated here:
$msg = $twitter->statuses->update("Browser-based Twitter clients are fun!");
For a complete breakdown of available API commands, see the Twitter API documentation.

About the Author

Jason Gilmore is the founder of the publishing and consulting firm He also is the author of several popular books, including "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Fourth Edition". Follow him on Twitter at @wjgilmore.