October 26, 2016
Hot Topics:

Integrating Twitter Into the Zend Framework

  • January 22, 2009
  • By W. Jason Gilmore
  • Send Email »
  • More Articles »

Create the News Controller

Next up is the creation of the controller used to retrieve the Twitter posts (or "tweets", as they're typically called). I've chosen the name "News", although you're of course free to use any name you please. Implementing the basic feature of logging in to the Twitter service and retrieving the latest tweets is done with remarkably little code:

Listing 1: The News Controller

class NewsController extends Zend_Controller_Action
   public function indexAction()
      // Retrieve the configuration handle
      $this->config = Zend_Registry::get('config');

      // Connect to the Twitter service
      $twitter =
         new Zend_Service_Twitter($this->config->twitter->username,

      // Retrieve a list of the user's latest 20 posts
      $this->view->posts = $twitter->status->userTimeline();

The default number of posts retrieved is 20, although you're free to change that value by passing the count parameter into the userTimeline() method:

$this->view->posts =
   $twitter->status->userTimeline(array("count" => 5));

Create the News View

Next up, you'll need to create the corresponding view. Although numerous pieces of post-related information are exposed via the Twitter service, for your purposes I've only referenced three (created_at, user->screen_name, and text). If you'd like to review what else is available, pass $this->posts into the var_dump() function.

Listing 2: The News View

<h1>The Latest Gaming News</h1>

<?php if (count($this->posts) > 0) { ?>
   <?php foreach($this->posts AS $post) { ?>

      On <b><?php echo date('m.d.y @ H:m:s',
         strtotime($post->created_at)); ?></b>,
      <b><a href="http://www.twitter.com/<?=
         $post->user->screen_name; ?>">

         <?= $post->user->screen_name; ?></a></b> said:<br />
      <?php echo ereg_replace("[[:alpha:]]+://
         "<a href=\"\\0\">\\0</a>", $post->text); ?>

   <?php } ?>
<?php } ?>

Automating Posts

Although not yet integrated into GameNomad, I've been thinking about automating posts that identify the currently hottest game as identified by Amazon.com's sales ranking system (GameNomad tracks the sales ranks for more than 500 games). Via a cron job, the sales ranks are updated every six hours and inserted into the GameNomad database. Via the Zend_Service_Twitter component, updating the GameNomad Twitter account with this information is really easy; just connect to the service and call the status->update() method:

$this->config = Zend_Registry::get('config');
$twitter =
   new Zend_Service_Twitter($this->config->twitter->username,
$response = $twitter->status->update(Hottest game of the moment:
   Call of Duty: World at War);

This feature will soon be integrated into the site, so stay tuned!

Where to From Here?

Integrating Twitter into your website using the Zend Framework's Zend_Service_Twitter component is a great example of this popular framework's power. Check out these links for more information regarding how to continue the integration process!

About the Author

Jason Gilmore is founder of a publishing, Web development, and consulting firm based out of Columbus, Ohio. Formerly Apress' open source editor, Jason fostered the development of more than 60 books, along the way helping to transform their open source line into one of the industry's most respected publishing programs. He's the author of several books, including the best-selling Beginning PHP and MySQL: From Novice to Professional (currently in its third edition), Beginning PHP and PostgreSQL: From Novice to Professional, and Beginning PHP and Oracle: From Novice to Professional.

Jason is co-founder of CodeMash, a nonprofit organization tasked with hosting an annual namesake developer's conference, and was a member of the 2008 MySQL Conference speaker selection board.

Tags: Twitter

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel