Adding a twitter feed for multiple accounts using Zend_Service_Twitter

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.

Let’s get setup

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.

Zend_Service_Twitter

Now we need to get Zend up and running with twitter. Luckily, the Zend_Service_Twitter client makes it nice and simple:

        $token = new Zend_Oauth_Token_Access();

        $token->setParams(array(
            'oauth_token'        => 'YOURAUTHTOKEN',
            'oauth_token_secret' => 'YOURAUTHSECRET',
        ));
        
        $twitter = new Zend_Service_Twitter(array(
        'username'       => 'YOURTWITTERACCOUNTNAME',
        'consumerKey'    => 'YOURCONSUMERKEY',
        'consumerSecret' => 'YOURCONSUMERSECRET',
        'accessToken'    => $token
        ));

        // verify user's credentials with Twitter
        $response = $twitter->account->verifyCredentials();

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:

    /**
     * Public list status
     * @param array $params
     * @return Zend_Rest_Client_Result
     */
    public function listStatuses($params)
    {
        $this->_init();
        $path = '/1/lists/statuses.xml';
        $response = $this->_get($path, $params);
        return new Zend_Rest_Client_Result($response->getBody());
    }

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:

        $params['slug'] = 'YOURLISTNAME';
        $params['owner_screen_name'] = 'YOURTWITTERACCOUNTNAME';
        $params['count'] = 3;
        $statuses = $twitter->status->listStatuses($params);
        
        foreach($statuses->status as $count=>$status)
        {
            echo $status->text . "<br /><br />";
        }

 

Twitter updates

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.

 
Sign Up
comments powered by Disqus
Sign Up

Popular Tags

350x250

Need a web developer?

If you'd like to work with code synthesis on your next project get in touch via the contact page.