Are You Active on Twitter?

View Results

Loading ... Loading ...

Show the Twitter Love! :)

Use PHP to Twitter Your Tweets
I recently got an email from a reader asking me if I could help them with a small script to post updates to a Twitter account using PHP. I figured this might be something that a few others might be interested in, so I decided to post about it.

As most of you know, there are a multitude of options when deciding how to to Tweet to Twitter. If you would like to add this functionality to your site so that you don’t have to download an application to your computer, or visit another site, this might be something you would like.

Using the simple script below you, you can post updates to twitter. Please BE ADVISED: this script needs altered to run. As well as some extra code to add your desired functionality.

$username = 'myUserName';
$password = 'myPassword';
$status = urlencode(stripslashes(urldecode('This is a new Tweet!')));

if ($status) {
$tweetUrl = 'http://www.twitter.com/statuses/update.xml';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "$tweetUrl");
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "status=$status");
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");

$result = curl_exec($curl);
$resultArray = curl_getinfo($curl);

if ($resultArray['http_code'] == 200)
echo 'Tweet Posted';
else
echo 'Could not post Tweet to Twitter right now. Try again later.';

curl_close($curl);
}

Needed Alterations:

  • Change $username to YOUR username
  • Change $password to YOUR password
  • Edit $status to the Tweet that you want posted.

Code to Add:

  • Some HTML to Tweet dynamically. For example: Use a form like below and have $_POST['new_tweet'] update the $status variable.
  • More Filtering: It would probably be a good idea to add some more filtering to the status so that you can’t just post a blanks status etc. (I could add that easily, but then where would be your opportunity to learn!)
  • Use OOP Practices: Creating a Twitter Class out of the below procedural code would make it much more modular for you in the future. (Again, better for you to do this yourself. Feel free to contact me with any questions.)
  • Add Some Javascript: Adding some Javascript that counts the amount of characters in the Tweet field would be handy to have for your visitors.

Try it out for yourself (Your Twitter Username and Password are NOT being recorded):

Tweet from BrownPHP

Username:

Password:

Tweet:

Tweet from BrownPHP if you like to test it out for yourself. You can also just copy this code, and make the necessary changes. I just wanted to give a small example to give you an idea of what you can do. Let me know what you think about it and if you have any ideas for some code scripts that you would like to see.

If you enjoy using Twitter, you may want to take a look at our Twitter Tag Cloud Service that we provide. You can also download the scripts to run the script from your own site.

Addendum (05/12/10):

This code has been added to the BrownPHP Code Repository if you would like to download it. You can go straight to the download by clicking the BrownPHP Twitter Class

Addendum (09/16/10):

Alright everyone, ask and you shall receive! I added a new post going through the Twitter OAuth method using PHP. It’s a lot simpler than you might think. Check it out.