Using Zend Oauth to Connect to Twitter (Through Magento)
As we all know Magento comes fully equipped with the Zend framework – of which Oauth is a noticeable inclusion. Oauth is essentially the method of obtaining authentication to use API’s and such with the worlds most popular online applications such as Twitter and Facebook.
What I wanted to do today is show you how to connect to your twitter account using the Magento/Zend library – I’ll then show you how to post a tweet, you can then perhaps think of how to apply this somehow within your Magento installation (tweet a new product when it’s added using an observer? Tweet new discount codes?). Lot’s of possiblities!
Step 1. Set up your Twitter API stuff.
- Visit https://dev.twitter.com -> login using your twitter credentials
- Click on “My Applications” (menu near your face top right)
- Click on “Create a new Application”
- Fill in the details (I normally leave callbackurl blank – but you can fill it in if you like)
Now you should be taken to a page for your twitter application – click on settings and change application type to “Read and Write”.
Back on the homepage click “Create Access Token” – you now should have all the stuff you need!
Step 2. Implement the Zend Oauth Library in Magento
For arguments sake lets say we have a controller with an action that we are going to call looking something like this:
public function testtwitterAction()
{
die('hello');
}
So we’ve got a blank action that I can initiate using a url (in my case I’m going to www.domain.com/mymodulefrontname/index/testtwitter) and this just displays a blank page with ‘hello’.
Ok, now what we need to do is to include our Zend Oauth library (by the way I’m using Magento 1.6.2.0 in this scenario) and fill in the rest of our access keys like so:
public function testtwitterAction()
{
$oauthtkn = new Zend_Oauth_Token_Access();
$oauthtkn->setParams(array(
'oauth_token' => 'XXXXXX-XXXXXX', // Access Token
'oauth_token_secret' => 'XXXXXXXX' // Access Token Secret
));
$twitter = new Zend_Service_Twitter(array(
'consumerKey' => 'XXXXXXXX', // Consumer Key
'consumerSecret' => 'XXXXXXXX', // Consumer Secret
'accessToken' => $oauthtkn
));
return $twitter->status->update('Testing a new tweet from Zend Oauth on #Magento! via @magentofox');
}
Now when I refresh my controller the function will be called, the twitter class should initiate a status update and I should post something to twitter!
And here we go:
That’s all there is to it really – now the hard part is putting it into something useful!
If you are struggling with the access – ensure that your settings are set to read & write – and if you have changed them make sure you re-create your access token!
Remember – Oauth is pretty universal – and the Zend Library is full of useful stuff – why not check out the other applications that are in there too and see what you can create!
Thanks for visiting the Magento Blog!
Rob is Ecommerce Web Design’s lead PHP developer, and an expert at customising the Magento framework to create completely unique sites. Follow him on twitter (because he's a twit) http://twitter.com/kent_robert. - Read my other posts.

