Friday, October 01, 2010

Single User Zend_Service_Twitter

When running a website for yourself or your company based on Zend Framework, you might want to show the Twitter messages to your audience.

Although the Zend Framework manual extensively describes how to set up a true Twitter application with the new OAuth implementation of Twitter using Zend_Service_Twitter and Zend_Oauth (since ZF-1.10.0), this is not what you're looking for. You need a simple approach, using the single user OAuth implementation of Twitter.

Registration at Twitter.
Register your "app" to twitter at http://dev.twitter.com/apps/new where you'll be presented a registration form for your app. Since it's not really an app, you should register your website as the app.
Once registered, you need to accept the general terms for usage of the Twitter API.


Once accepted, your applications registered and you can start implementing it. But for a single user approach, you need to retrieve your access tokens.
Fetching tweets
Now it's time to fetch those tweets and post them on your website.

Since these tokens are config elements, we store them in our application.ini as we keep this as our main configuration file for our whole website.

[edited 10/3] These configuration settings are bogus and won't work on my personal account!

; Twitter service
service.twitter.oauth.username = "DragonBe"
service.twitter.oauth.oauth_token = "1234567-xPhPanDz3nDfRam3W0RkN0wW1THS1nGl3Z3nd0aUth"
service.twitter.oauth.oauth_token_secret = "8tH1sStr1nGmUstB3sT0r3ds0MwH3r3V3RryS3cr3t"

Now a simple model can be used to set up the verification process and retrieve the Twitter instance.

<?php
class Application_Model_TwitterClient
{
protected $_config;
protected $_twitter;

public function __construct()
{
$config = new Zend_Config_Ini(
APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
$this->_config = $config->service->twitter->oauth;
$this->_twitter = new Zend_Service_Twitter();
}

public function authenticate()
{
$accessToken = new Zend_Oauth_Token_Access();
$accessToken->setToken($this->_config->oauth_token)
->setTokenSecret($this->_config->oauth_token_secret);

$this->_twitter->setLocalHttpClient(
$accessToken->getHttpClient($this->_config->options->toArray()));
return $this->_twitter->account->verifyCredentials(); } public function getTwitter() { return $this->_twitter; } }

Now we just need to call this model in our controller to retrieve the messages themselves

<?php
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$twitterClient = new Application_Model_TwitterClient();
$result = $twitterClient->authenticate();
$twitter = $twitterClient->getTwitter();
$status = $twitter->status->userTimeline();
$this->view->statusMsgs = $status;
}
}

And in your view you just loop through those status messages

<li class="myTweetList">
<?php foreach ($this->statusMsgs as $tweet): ?>
<li><?php echo $this->escape($tweet->text); ?></li>
<?php endforeach; ?>
</ul>

Of course you need to add your own validation and caching to this code. But in a nutshell this works pretty damn good.

I've even build a model around retrieving those twitter messages and the actual properties of a Twitter status, but this is beyond the scope of this article.

If you find this information useful, or if you have a better approach, let me know in the comments.
Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 License.