How To Update Your Twitter Status With PHP and OAuth

In my last post I showed you how to make a sign in with twitter system and get a user’s info, in this post I will show you how to update a user’s status.

We will be using the same files as we did last time so let’s pick up from were we left, that would be the “profile-page” file.

Notice the following line in that file.

$user= $Twitter->get_accountVerify_credentials();

Now go to twitter’s api page where it shows you the url to get a user’s information.

Twitter REST API Method: account verify_credentials

In this page twitter gives us a list of things, but the ones you have to pay attention to are  the URL, HTTP Method and Response.

URL:

http://twitter.com/account/verify_credentials.format


HTTP Method(s):

GET

Now that you’ve seen this page let’s take a look at the previous line of code again and dissect it.

$user= $Twitter->get_accountVerify_credentials();

See how it seems like we separate words with an underscore ( _ )? that’s because that’s exactly what we do! The word before the first underscore is the HTTP Method of the API method you want to use, if the HTTP Method was POST we would have used “post_accountVerify_credentials()” instead of “get_accountVerify_credentials()”.

What follows after the first underscore is the URI or the rest of the URL after http://twitter.com/

For the “account verify credentials” method we have the URI account/verify_credentials, the format does not matter since we are using Epi and it returns an xml object, in our line of code we turned this URI into “accountVerify_credentials” , this means we removed the forwardslash “/” from the URI and capitalize the word after, in this case “verify”.

So from now on think of the previous code this way.

$xmlResponseObject= $EpiTwitterObject->method_uri();

Now you know the syntax of how the method we used last time worked, let’s check out twitter’s status update method.

Twitter REST API Method: statuses update

The url, http methods  and paramaters are

URL :

http://twitter.com/statuses/update.format

HTTP Method(s):

POST

Parameters: Status (required)

This means that our update status code will look like this.

$status=$Twitter->post_statusesUpdate(array('status'=>'hello world!'));
$status->response;

To implement AJAX status updates with JQuery in your “profile-page.php” file add the following code to it.

<div id="response">
      <-- server response -->
</div>

<div>
   <-- update status field and button-->

   <input type="text" id="status" />
   <button id="update">Update</button>
</div>

<-- load jquery -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
var status
	$("#update").click(function(){
		status=$("#status").val();
		$.post('update.php',{
			status:status
		},function(response){
			$("#response").html(response);
		});
	});
});
</script>

And make a filed called “update.php” with the following content.

include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
include 'keys.php';
$Twitter = new EpiTwitter($consumerKey, $consumerSecret);
$Twitter->setToken($_COOKIE['oauth_token'],$_COOKIE['oauth_token_secret']);

//validate here
$text=$_POST['status'];
$status=$Twitter->post_statusesUpdate(array('status' => $text));
$status->response;
echo 'Your status has been updated';

That’s it for this tutorial, now go make some twitter apps!

blog comments powered by Disqus