Tag: twitter status

Use PHP to Twitter Using OAuth

Ever since Twitter stop allowing status updates to be posted with some simple php code, my Twitter script has been a big fail whale for everyone. In effort to keep the BrownPHP group happy, here is a little tutorial to post status updates to Twitter with PHP via OAuth.

Link to the BrownPHP Twitter Status Update using OAuth.

Open Authentication Standard Protocol

Now I am not going to get into all the details of “OAuth” or the Open Authentication protocol standard. You can learn more about OAuth and all of its goodness at the OAuth website.

Twitter Authentication Flow Overview

Below is the Flow Overview listed in their documentation that is actually quite helpful to understand the process of authenticating yourself with Twitter.

  1. Build TwitterOAuth object using client credentials.
  2. Request temporary credentials from Twitter.
  3. Build authorize URL for Twitter.
  4. Redirect user to authorize URL.
  5. User authorizes access and returns from Twitter.
  6. Rebuild TwitterOAuth object with client credentials and temporary credentials.
  7. Get token credentials from Twitter.
  8. Rebuild TwitterOAuth object with client credentials and token credentials.
  9. Query Twitter API.

See! Nothing to it!

Pre-Requisites

Once you have completed the Pre-Requisites. Most of the work is actually done.

Folder/File Review

In the twitteroauth code that you have downloaded (and hopefully uploaded to your web server by now) this is the structure that you should see. Included are two folders; images and twitteroauth. Images contains two png files that say, “Sign in with Twitter” that you are free to use with your applications. The twitteroauth folder contains the files that enable the Twitter black magic.

OAuth.php contains many classes used for the OAuth standard. As mentioned above, the goal of this article isn’t to explain OAuth, so just take my word and do not remove this file. The twitteroauth.php file contains the TwitterOAuth class that includes all the methods that you will need for this to work.

config.php is where you will put your consumer key and consumer secret that you should have now after your registered your application with Twitter. Also in this file you will update the OAUTH_CALLBACK define with the url that you want users to be redirected to after they return from Twitter.

callback.php should be the file that you have your config.php file configured to return from Twitter with. This file builds the TwitterOAuth object after the user returns from Twitter.

clearsessions.php does exactly says. It clears the sessions (this will remove the authentication tokens) that have been created and redirects the user to connect.php. connect.php is the start of the example. It basically only has a button (lighter.png/darker.png) to “Sign In with Twitter” that links to redirect.php.

redirect.php is where everything gets started. This file builds the temporary credentials and sends them over to Twitter for authentication. Twitter should then redirect home to the callback.php and then that will redirect to index.php which displays all the information of the user that just logged in. This is nice for showing off all the info that you can pull directly after authenticating with Twitter without having to send any separate requests.

DOCUMENTATION, html.inc, LICENSE, README and test.php are all helpful to skim over, but not really needed for this post.

Link to the BrownPHP Twitter Status Update using OAuth.


Use PHP to Twitter Your Tweets

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.


  • BrownPHP Tag Cloud

  • Copyright © 1996-2010 Brown PHP. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress

    Powered by Yahoo! Answers