Using Zend_Service_Twitter and OAuth

I’ve just spent all afternoon trying to update a very simple Twitter client to post status updates to Twitter. The code has been using BASIC authentication to post Tweets until now but seeing as that’s about to be discontinued in favour of OAuth I thought I’d better update things. I had no problem using Zend_Oauth_Consumer to get an access token and an access token secret but the problems started when trying to use Zend_Service_Twitter.

The first problem came because the online version of the Framework documentation hasn’t been updated with the latest release. As Zend_Service_Twitter has been largely refactored to use OAuth this is a major issue (the constructor even accepts different arguments from what the documentation says). At no point is it clear what arguments need to be passed to the constructor or in what order. Anyway, after hours of frustration I finally came by some help on StackOverflow. Here’s some very simple test code to post a Tweet using Zend_Service_Twitter and OAuth (assuming you’ve already registered an application with Twitter and got the correct user authorisation keys):

$access = new Zend_Oauth_Token_Access();
 $params = array(
 'accessToken' => $access,
 $twitter = new Zend_Service_Twitter($params);
 $twitter->statusUpdate('Testing posting tweets using OAuth!');

The statusUpdate method returns an instance of Zend_Rest_Client_Result, which can be checked to see if the request was successful. The method also throws Zend_Http_Client_Exception and Zend_Service_Twitter_exception, which I would of course catch in production code.

<rant>This has taken me hours to figure out. If only the documentation had been properly updated this should have been a cinch to put together. I am a newb at Zend Framework but I think this would have stumped anyone. Please, please, please get this documentation sorted out!</rant>

Anyway, I hope this helps someone and stops them from facing the hours of frustration I just have.

8 thoughts on “Using Zend_Service_Twitter and OAuth

    1. Glad to help! I just looked at the documentation for Zend Framework again and while it’s been updated it assumes that everyone wants to serialise the object returned by Zend_Oauth to use later. There’s nothing to explain how to create the necessary objects from scratch and how to pass them to Zend_Service_Twitter. This seems to be a big oversight to me. What do you think?

  1. Thanks a lot man, it worked perfectly.

    I’ve now an automatic status update for each new question that is created on (which is the project where I’m currently working on).

    I appreciate it 😉 !

  2. Does this still work ? I’m using this code and I always have a “Could not authenticate with OAuth.” returned by updateStatus…

  3. I don’t know. I’ve seen many posts on StackOverflow as well as DragonBe blog post where the same approach is shown. But it doesn’t want to work here…. I’ve tried to regenerate my tokens several times, but no luck so far…

    1. The error comes from ZF 2 Twitter service, as it works well with ZF 1. There must be an error somewhere in the ZF 2 base code.

Leave a Reply