How would we go about creating a twitter feed for a web site that pulled the latest statuses or ‘tweets’ from several different accounts using Zend’s Zend_Service_Twitter client? This is the question I was looking at last week, specifically using Zend version 1 and the Twitter API version 1.
Adding a twitter feed to your web site is a great way to make it more interactive. Twitter ‘widgets’ allow you to easily achieve this, but when you want something a bit more customised the twitter API has a lot to offer.
Pulling a feed from a single account is pretty straight forward, but how do we pull from multiple accounts? One way would be to make an API call to each account and merge the latest statuses, however in my case where I needed to pull from ten or more different accounts, this isn’t very practical or efficient. An improved solution is to make use of twitter’s ‘list’ functionality. By setting up a global twitter account and creating a ‘list’ of your other accounts, it’s possible to pull the latest updates from a range of different accounts without the hassle of having to merge feeds.
So to start with, create an account on twitter and create a list by going to Home > Tweets >Lists > Create List. Give the list a name, make it public and then add as many accounts to it as you would like to be in the feed.
Next you’ll need your keys and tokens to use the API. To get this, go to the twitter development site.
Now we need to get Zend up and running with twitter. Luckily, the Zend_Service_Twitter client makes it nice and simple:
With these few lines we now have access to all the methods to interact with the Twitter API using the $twitter variable.
Unfortunately it doesn’t seem that Zend has a method to retrieve list statuses, though this is built into the Twitter API. So we’ll need to make a small update to the Zend_Twitter_Service. To do this, open up Library/Zend/Service/Twitter.php and add in the following function:
So to use this function, if we jump back to our first file and add the following code you’ll see we can now access the statuses of lists:
And that’s it! We’re now pulling back status updates from our twitter list. Note that the ‘count’ variable lets you choose how many tweets it will bring back, there’s also a range of other variables you can pass in if you want to, which you can read about in the twitter documentation. The $status variable contains all the other variables you need including the user data and when the tweet was posted.