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 Arrays - Associative Arrays or Hash Maps

Associative array or hash maps are listings of key and value pairs with a posibility to nest additional keys and values. An associative array is a very powerful construct within PHP.

In our previous article we discussed simple arrays, which in their turn are indexed associative arrays under the hood. Take the following example:

$array = [

Is in fact an indexed associative array under the hood:

$array = [
0 => 'apple',
1 => 'banana',
2 => 'chocolate',

But associative arrays can be so much more than just an indexed array, and you will find many database operations returning arrays where the fields of a table are the keys in the array while their values are also the values within the array.

$productRowData = [
'product_id' => 1234,
'brand_id' => 321,
'product_name' => 'Our awesome product',
'prodcut_description' => 'This is our most awesome product.&#…

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

Deploy Docker containers fast to Microsoft Azure

DEPLOY DOCKER CONTAINERS FAST TO MICROSOFT AZURE It’s hard to ignore the fact thatDockeris a way to move forward for rapid application development, distributed architectures and microservices. For developersDockeroffers great advantages as they can build their containers specifically for the task they work on. They grab a base image of a container, modify it for their purpose and prepare the functionality inside the container. Quality, testing and security teams now have a single instance to look at and ensure all functional and regulatory requirements are met. System engineers now don’t have to worry about providing a system with the required specs as the container is already provisioned for that purpose. But where do you deploy yourDockercontainers? You can set up your existing bare metal infrastructure to allow them to run containers, but this also means you need to learn about securing your container infrastructure, which is not an easy task. Luckily “the cloud” offers container …