Zend Framework Twitter and oAuth in 1.10.7

Code, oauth, php, twitter, Zend Framework

Today I wanted need to switch out some implementations I have in twitter. I was using HTTP Basic for authentication as were most. I went through the Zend Oauth docs, and combined with their examples and those of Pádraic Brady I got this working fine.

I’m not going to give a step by step, as I’m assuming you’ve already read various docs.

I broke my files out like Pádraic, so if you’ve read his than you’ll understand mine.

First, my config file which is included within each subsequent file.

$config = array(
        'callbackUrl'    => 'http://www.example.com/callback.php',
        'siteUrl'        => 'http://twitter.com/oauth',
        'consumerKey'    => 'XXXXXXXXXXXXXXXXXXXXX',

My index page is the starting point of the authentication process. Our persistent storage for this example is in session, though in production I’m using a database.

include_once './config.php';

    // $config is from our include!
    $consumer = new Zend_Oauth_Consumer($config);

    // fetch a request token
    $token = $consumer->getRequestToken();

    // persist the token to storage
    $_SESSION['TWITTER_REQUEST_TOKEN'] = serialize($token);

    // redirect the user

The callback url specified will now be triggered

include_once './config.php';

// Check if _GET is set and that the request token is being returned
if (!empty($_GET) && isset($_SESSION['TWITTER_REQUEST_TOKEN'])) {

    $consumer = new Zend_Oauth_Consumer($config);
    $token = $consumer->getAccessToken($_GET, unserialize($_SESSION['TWITTER_REQUEST_TOKEN']));

    $_SESSION['TWITTER_ACCESS_TOKEN'] = serialize($token);

    // Now that we have an Access Token, we can discard the Request Token
} else {
    // Mistaken request? Some malfeasant trying something?
    exit('Invalid callback request. Oops. Sorry.');

// Lets just pass on to a test posting page

Finally, if all went to plan and the user authorized our application, we can pull in their access token and do as we need on their behalf.

include_once './config.php';

// Verify we're good to go

    // Unserialize and add the token to the config
    $token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']);
    $config['accessToken'] = $token;

    // Pass the config into Twitter, and act upon the users account
    $twitter = new Zend_Service_Twitter($config);

    $twitter->status->update('I am so posting from your account. Rock!');

That’s all really. I do wish the Zend Service Twitter docs would be updated to reflect some examples. I saw in SVN Pádraic has already changed some of the docs, they’ve just not been updated on site yet.

Thanks to the documentation authors and those I borrowed from to get my working example going and so I could more easily move forward with this project.


No Comments

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>