Zend OAuth With Twitter: Updating A User’s Status

Welcome to part three of these series, for those who are the new: in the first post you learnĀ how to make the sign in with twitter button, in the second post I show you how to authenticate users. I put them in separate posts because I don’t like long posts so they are pretty short.

In this post we will make a text box with a button that will let users update their status from your site. Since we will be using JQuery you should be familiar with JQuery’s AJAX methods.

The User Interface

.

The app's interface: A text box and update button

Now that we know how the app is going to look like let’s look at its code.

	 <textarea id="status">

	 </textarea>
	 <button id="update">Update</button>
	 <div id="response"></div>

This doesn’t do anything of course so let’s add some JQuery.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	 // make the variable for the status
	 var status;
		// detect clicks
		$("button#update").click(function(){
		// when the users clicks the button
			// get the status from the textbox and store in the status variable
			status=$('textarea#status').val();
			// POST to the updatescript with the status parameter
			$.post('updatestatus.php',{status:status},function(response){
			    // display the response in the response div
				$('div#response').html(response);
			});
		});
});
</script>

The user interface is still part of your callback script so let’s go ahead and add to it.

The full callback script:

<?php
session_start();
require_once 'Zend/Oauth/Consumer.php';
require_once 'oauthConfig.php';

$oauth = new Zend_Oauth_Consumer($config);

if (isset($_GET['oauth_token']) && isset($_SESSION['request_token'])) {
    try{
		$access = $oauth->getAccessToken($_GET, unserialize($_SESSION['request_token']));
	}
	catch(Exception $e)
	{
	 echo 'Error: '.$e->getMessage();
	 exit (1);
	}
    $_SESSION['access_token'] = serialize($access);
     $_SESSION['request_token'] = null;
	 ?>
	 <html>
	 <body>
	 <textarea id="status">

	 </textarea>
	 <button id="update">Update</button>
	 <div id="response"></div>
	 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
	 <script type="text/javascript">
	 $(document).ready(function(){
	 // make the variable for the status
	 var status;
		// detect clicks
		$("button#update").click(function(){
		// when the users clicks the button
			// get the status from the textbox and store in the status variable
			status=$('textarea#status').val();
			// POST to the updatescript with the status parameter
			$.post('updatestatus.php',{status:status},function(response){
			    // display the response in the response div
				$('div#response').html(response);
			});
		});
	 });
	 </script>
	 </body>
	 </html>
	 <?php

}elseif(!empty($_GET['denied'])){
	echo 'you have denied us access to your twitter crendentials';
}
else {
    echo 'Invalid callback request. Oops. Sorry.';
}
?>

Updating The User’s Status

Like with all our scripts our update script will need the sessions, oauth class and configuration file. This time however, we will also need Zend’s JSON class.

<?php
session_start();

require_once 'Zend/Oauth/Consumer.php';
require_once 'Zend/Json.php';
require_once 'oauthConfig.php';

We are ready to call any of twitter’s API methods, but before we do so we need to know how to call them. Take a look at the method needed to update a user’s status and the requirements. I have circled the stuff we will use.

API Method Used To Update Your Status

You also need to see the response. Note that although we are using JSON the response will be the same. We will use “text”, your new tweet, to check if the updating went through.

Twitter API Update Response, Text Represents Your New Tweet

At last! This is the full update status script (updatestatus.php) which was called through JQuery from your callback script.

<?php
session_start();

require_once 'Zend/Oauth/Consumer.php';
require_once 'Zend/Json.php';
require_once 'oauthConfig.php';

// get status from callback script
$statusMessage =$_POST['status'];
// get access token from session
$access_token = unserialize($_SESSION['access_token']);
// configure client
$client = $access_token->getHttpClient($config);
// set twitter's update url
$client->setUri('http://api.twitter.com/1/statuses/update.json');
// specify method
$client->setMethod(Zend_Http_Client::POST);
// set parameters
$client->setParameterPost('status', $statusMessage);
// send request
$response = $client->request();
// decode response
$data = Zend_Json::decode($response->getBody());
$result = $response->getBody();
// check if status was updated
if (isset($data->text)) {
	// status updated :)
    $result = 'profile updated';
}else{
// failed to update status :(
$result='failed to update';
}

echo $result;

?>

Logging Out Of Your Application:

Logging out of your application is pretty simple, all you need to do is destroy the sessions.

if(isset($_SESSION['access_token']))
{

	$_SESSION = array();
	session_destroy();
}

echo 'you have logged out';

This won’t log you out of twitter though. I tried calling the method end_session in the API but that didn’t work so be sure to let your users know that they are still signed in to twitter.