How to update your twitter status using php

by krike in General on 23 Jan 2011


Download php class

First of all you will need to download the class created by Tijs Verkoyen.

Once downloaded unpack it on your computer. Also open the documentation page for later use (It’s the page with all the available functions)

Setup the project

Let’s first create a new project, I will be using MAMP. Create a new folder named tweeter inside the root directory of your MAMP.

In that folder let’s create a new file named index.php.

The next step is to add the class and already include it in our index.php file. So create a folder classes and copy/paste the twitter class in that folder

Now we need to include the file in our index.php

<?php
//include twitter class
include('classes/twitter.php');
?>

Create twitter application

Before we continue we must Register a new API, we need to do this if we want to communicate with twitter. Go to http://dev.twitter.com/apps, click create new application and follow the onscreen steps. Make sure to select read and write.

Make sure to take note of your Consumer key and Consumer Secret, we will need it later.

Get your twitter tokens

Next thing we must do is get the twitter tokens to authenticate before requesting or sending information to twitter. Go to http://dev.twitter.com/apps, click on the “my tokens” button and copy your tokens.

Working with the php class

Let’s create a new instance of the class twitter, when you go through the documentation you will find that statusUpdate() the function is that we need. When creating a new instance of the twitter class you need to pass your consumer key and consumer secret as parameters. (replace ‘your consumer key’ and ‘your consumer secret’ with the keys you copied when you created your twitter application)

$twitter = new Twitter('your consumer key', 'your consumer secret');

Set your twitter tokens so you authenticate and have access to your twitter profile. (replace ‘‘ and ‘‘ with the tokens you copied earlier)

// set tokens
  $twitter->setOAuthToken('<your-token>');
$twitter->setOAuthTokenSecret('<your-token-secret>');

Now let’s create a new variable with some text.

$tweet = "This tweet was posted from a custom php script";

And pass the variable as a parameter to the statusUpdate() function

$twitter->statusesUpdate($tweet);

If you are sure you followed every step carefully you can browse to http://localhost:8888/tweeter/index.php. The only thing you need to do now is check your twitter account to see if the script added the new tweet.

The code wrapped up

<?php
//include twitter class
include('classes/twitter.php');
$twitter = new Twitter('your consumer key', 'your consumer secret');
// set tokens
$twitter->setOAuthToken('<your-token>');
$twitter->setOAuthTokenSecret('<your-token-secret>');
$tweet = "This tweet was posted from a custom php script";
$twitter->statusesUpdate($tweet);)
?>

Conclussion

It’s so great and easy what you can do with the class written by Tijs Verkoyen we can now easily update our twitter timeline using PHP in only a few easy steps. All you need to do is register an app, get the tokens, include the class, set the tokens and pass the text you wish to tweet.

Written by krike

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sit amet nisl nisl. Ut interdum libero vitae quam ultricies et lacinia elit aliquet. Praesent tincidunt, sem tempus feugiat feugiat, turpis tellus scelerisque erat, sit amet feugiat neque arcu ac lectus. Sed at mi et elit interdum scelerisque vitae eu felis.

krike has written 77 posts.

  • Alex

    How I hate such posts! Just copy paste from other blogs! This script doesn’t work! When you run it you’ll get an error message “Call to undefined method statusUpdate()”

    Damn

    • I implemented this into one of my projects and thought it would be usefull to share. It was never my intention to copy paste. I had to search for to long how to integrate twitter and therefore decided to write this article. You say it doesn’t work, did you at least took the time to debug your code on your own to see what was wrong. I will review this tutorial to see if i missed something. But then again, why would I even bother when you ask it so nicely. But yeah … It’s so much easier to critizise people

  • The code, as posted, doesn’t work because there is no “statusUpdate()” function in the Twitter class. It should be “statusesUpdate()”. 🙂

    • @Ryan: thanks, small typo 🙂

  • adin234

    i tried it but it didn’t update my twitter status :/

    i followed the instructions :/

    • @adin234: sorry for my late response. Could you send your files to idealvisions[at]live[com], i will have a look at it

  • Eko

    Dear krike,
    i tried it but it didn’t update my twitter status,
    I tried from localhost..

    i followed the instructions well..

    • @Eko: mail me your php file or project at krike06 [at] gmail [dot] com. Also let me know if it gives you some kind of error. It could be that your tweet is to long or something invalid was entered.

  • bank

    Thank you very much. I can do it now. ^^

    • @bank: glad to hear it

  • latestnews

    Boss! I have bunch of thanks for you! Thanks buddy! It really helped! Its really awesome! Thanks a bunch….. Keep it up!!

  • r->statusesUpdate($tweet);) got rid of that extra ‘)’ and it worked first time – great stuff.

  • Since there’s been many twitter changes, just wanted to find out if this process is still valid? Many thank!

    • @Amien: Our main site is using this library to post new tutorials automatically to twitter. I just did a quick test and it still works so all the info in this article is still valid. Happy coding 🙂

  • Some of the code in the twitter.php will not work on older servers with php 5.1.x. The json_encode and json_decode function calls were not introduced until 5.2.
    Additionally, curl_setopt_array(), also in 5.2.x., must be replaced with individual curl_setopt() calls. It is in 3 different places in the code.
    To work around, I used another (excellent) json library by Michal Migurski and replaced the json_decode calls with the appropriate calls in that library.
    All works well!!!

    • Ah yes I see. Thank you for sharing this.