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(); $access->setToken('USER AUTHORISATION TOKEN')->setTokenSecret('USER AUTHORISATION TOKEN SECRET'); $params = array( 'accessToken' => $access, 'consumerKey' => 'YOUR APPLICATIONS CONSUMER KEY', 'consumerSecret' => 'YOUR APPLICATIONS CONSUMER SECRET' ); $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.