Skip to main content

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 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.

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();

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

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; ?>

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.


  1. I hope the tokens you exposed here are not valid any longer, other wise, you should quickly invalidate them if you like to keep your account :-)

  2. Nice article, quick overview of the functionality!

    @pilif come on, do they look anywhere near real keys?

  3. @pilif,

    Just as Iwan pointed out, these keys are bogus and won't work (unless someone at Twitter puts a prank on me).

    But thanks for mentioning it, I added a small message explaining the configuration is bogus.

  4. Thanks for this article, I was searching a way to update my application which was using simple auth. and was not working any more.

    I had to modify a bit your code (the config come from a DB), but twitter return an error: Invalid Signature.

    I have double checked token and secret, I'm sure it's correct, and I can retrieve my timeline, but I can't update my status.

    On this post, it said that more parameters are need:

    Any idea about this?


  5. @pvledoux,

    I'm not sure about updating status messages using above code, but I've been using a simple twitter script to update our company's dev-box twitter account when someone commits to SVN.

    Check out for my example script (of course with bogus account details)

  6. Thanks a lot! In fact I was trying to inject my token (the string) as the accessToken (which is in fact an object). I better understand how it is suppose to work (or not) now!

  7. Anonymous6/10/10 14:11





  8. Very nice article if you look at the technique used. The only thing that I was wondering is why you prefer retrieving tweets on the application level of twitter when there is an RSS feed available for each twitter account?

    I can imagine to use this solution to connect and send status updates.

  9. @Martin,

    I posted this article since I already had a working Twitter client that needed to be converted to use with their change to OAuth and that the Zend_Twitter manual only explains a full application approach and not a single user usage of the API.

    Besides, using the API you can do so much more than with a simple RSS feed.

  10. Anonymous above is right, there is an ->options-> too many in the code. In the authenticate() function, the source layout breaks at the end slightly.

    Apart from that, the code seems to run, but I'm consistently getting a 'could not authenticate you' from Twitter..

  11. Fixed my problem; for people struggling with the same issue, in the above tutorial 2 options are missing. Make sure your options also contain the consumer key and secret, which is necessary to sign the requests that are sent to Twitter.


    service.twitter.oauth.consumerKey = 'your consumer key';
    service.twitter.oauth.consumerSecret = 'your consumer secret';

  12. @Ivo,

    Hey thanks. Apparently I forgot to put them in here, cause I have those set in my configuration.

    But thanks for your feedback, much appreciated.

  13. Hey Thanks, single user are a large part users of twitter

  14. Anonymous7/5/11 14:50

    Yours tips help me. I used them on

  15. Anonymous18/1/12 20:18

    Thanks for the tips above. This saved me a ton of work - Thanks!


Post a Comment

Popular posts from this blog

PHP 7 and Apache on macOS Sierra

I posted several talks about compiling PHP from source, but everyone was trying to convince me that a package manager like Homebrew was a more convenient way to install. The purpose of Homebrew is simple: a package manager for macOS that will allow you to set up and install common packages easily and allows you to update frequently using simple commands. I used a clean installation of macOS Sierra to ensure all steps could be recorded and tested. In most cases you already have done work on your Mac, so chances are you can skip a few steps in this tutorial. APACHE AND PHP WITH HOMEBREW I’ve made this according to the installation instructions given on GetGrav. The installation procedures These installation procedures will set up your macOS Sierra with PHP 7.1 and Apache 2.4. Install Xcode command line tools (if not done yet)xcode-select --install Install Homebrew/usr/bin/ruby -e "$(curl -fsSL" Set up for in…

Sessions in PHP 7.1 and Redis

In case you have missed it, PHP 7.1.0 has been released recently. Now you can’t wait to upgrade your servers to the latest and greatest PHP version ever. But hold that thought a second… With PHP 7 lots of things have changed underneath the hood. But these changed features can also put unexpected challenges on your path. Our challenge One of these challenges that we faced was getting PHP 7.1 to play nice storing sessions in our Redis storage. In order to store sessions in Redis, we needed to install the Redis PHP extension that not only provides PHP functions for Redis, but also installs the PHP session handler for Redis. Because we upgraded our servers to PHP 7.1, we were looking to use the latest provided version for this Redis extension: redis-3.1.0. Once installed, we bumped against a nasty problem. Warning: session_start(): Failed to read session data: redis (path: tcp:// Searching the internet for this error, we didn’t got many hits that could point us into a dire…

Speeding up database calls with PDO and iterators

When you review lots of code, you often wonder why things were written the way they were. Especially when making expensive calls to a database, I still see things that could and should be improved.
No framework development When working with a framework, mostly these database calls are optimized for the developer and abstract the complex logic to improve and optimize the retrieval and usage of data. But then developers need to build something without a framework and end up using the basics of PHP in a sub-optimal way.

$pdo = new \PDO( $config['db']['dsn'], $config['db']['username'], $config['db']['password'] ); $sql = 'SELECT * FROM `gen_contact` ORDER BY `contact_modified` DESC'; $stmt = $pdo->prepare($sql); $stmt->execute(); $data = $stmt->fetchAll(\PDO::FETCH_OBJ); echo 'Getting the contacts that changed the last 3 months' . PHP_EOL; foreach ($data as $row) { $dt = new \DateTime('2015-04-…