How to update your Twitter status using Zend Framework

After reading the NetTuts tutorial on how to update your Twitter status using CodeIgniter, I wanted to show how to update your Twitter status using Zend Framework.

For this tutorial you need to install Zend Framework and Zend_Tool first.

Step 1: setup apache vhost by creating the /etc/apache2/sites-available/twitter as follows:

<VirtualHost *:80>

    ServerAdmin [email protected]
    DocumentRoot /home/mimir/Zend/workspaces/DefaultWorkspace7/twitter
    ServerName twitter.dev
    ServerAlias www.twitter.dev
    ErrorLog /home/mimir/Zend/workspaces/DefaultWorkspace7/logs/twitter_error_log
    CustomLog /home/mimir/Zend/workspaces/DefaultWorkspace7/logs/twitter_access_log combined

    <Directory "/home/mimir/Zend/workspaces/DefaultWorkspace7/twitter/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
    </Directory>

</VirtualHost>

You will need to change the DocumentRoot and Directory directives to match your configuration.

Step 2: add twitter.dev to your hosts file:

127.0.0.1	twitter.dev www.twitter.dev

Step 3: create the Zend Framework project

# i switch to my Zend Studio workspace, where apache is reading his sites from:
# cd Zend/workspaces/DefaultWorkspace7/
zf create project twitter

Step 4: restart apache

sudo /etc/init.d/apache2 restart

At this point you should have a default Zend Framework project that you can browse at http://twitter.dev/public/. How let’s change our status :)

Replace the content of IndexController with the following:

<?php
class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // we create the form
        $updateForm = new Zend_Form();

        $status = new Zend_Form_Element_Text('status');
        $status->setLabel('New Twitter Status')
               ->setRequired(true)
               ->addFilter('StripTags')
               ->addFilter('StringTrim')
               ->addValidator('NotEmpty');

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Update');

        $updateForm->addElements(array($status, $submit));

        // we send the form to the view
        $this->view->updateForm = $updateForm;

        // we check if there was any POST
        if ($this->getRequest()->isPost()){
            $formData = $this->_request->getPost();
            // checking if the form data is valid (if we have a new status or not)
            if ($this->view->updateForm->isValid($formData)){
                // our form is valid, we can update our status
                $twitterStatus = $formData['status'];

                $twitter = new Zend_Service_Twitter('myusername', 'mypassword');
                $response = $twitter->status->update($twitterStatus);
            }
        }

    }

}

Also replace the view for the index action of IndexController with:


<h1>Welcome to the Twitter Update Tutorial</h1>

<?php echo $this->updateForm?>

TADA! You can now check your twitter account and see that twitter status is updated every time you submit the form -:)

3 thoughts on “How to update your Twitter status using Zend Framework”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>