Jun 22 2009

Update Twitter using Command Line, Javascript, Or PHP.

Everyone seems to be all about Twitter so here’s some simple examples of how to update your Twitter status from a command line prompt, web server or simple html web site. These three examples require curl so install it if you don’t already have it. For these examples I’ll be using my Twitter user name ‘codytaylor1234’. My password is not ‘mypassword’ so make sure you put in your own information.

The easiest way to update your Twitter account is to just call curl from the command line with this command.


curl --basic --user "codytaylor1234:mypassword" --data-ascii 
"status=This Twitter update brought to you by curl on the command line" 
"http://twitter.com/statuses/update.json"


To update your Twitter status with PHP you are going to want to do the same sort of thing but with a bit more typing.

<?php
$username = 'codytaylor1234';
$password = 'mypassword';

$update = 'This Twitter update is from a php script using curl';

$url = 'http://twitter.com/statuses/update.json';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "status=".$update);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
$result = curl_exec($ch);
curl_close($ch);

if($result)
    echo 'success';

?>;

Since a cross-domain request in Javascript isn’t really an option we have to create a proxy using PHP in order to authenticate the user on the Twitter API. If anyone knows an easy way authenticate a Twitter user using only javascript I’d love to hear it. Anyway if we replace a small amount of code in the above example and put it in a file then we can use a simple ajax request to update our Twitter status. So the new PHP file would be:


<?php
$username = $_POST['username'];
$password = $_POST['password'];

$update = $_POST['update'];

$url = 'http://twitter.com/statuses/update.json';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "status=".$update);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
$result = curl_exec($ch);
curl_close($ch);

?>

For this example I called that php file ‘twitter-update.php’. Now that we have our simple proxy we can update our twitter status with a simple html form and a little ajax. I used the prototype framework for my javascript.


<script src="includes/prototype.js" type="text/javascript">
<script type='text/javascript'>
function update_twitter()
{
  var param_string = "username="+$('username').value+"&password="+
        $('password').value+"&update="+$('update').value;
  alert(param_string);
  var options = {
    method: "post",
    parameters: param_string,
    onSuccess: function (xhr, Json) {
      alert("Response received successfully.");
    },
    onFailure: function (xhr, Json) {
      alert("Request was unsuccessful.");
    }
  };
  
  var oRequest = new Ajax.Request("twitter-update.php", options);
}

</script>

Obviously this is for example purposes only and if you’re actually using it for production then you should edit it a lot. Now for the last little bit here’s the simple html form that starts it all.


User Name : <input type='text' id='username' value='codytaylor1234'><br>
Password  : <input type='password' id='password' value='mypassword'><br>
New Status : <input type='text' id='update' 
value='Twitter Update from html/javascript/php'><br>
<input type='button' value="Update Twitter" onclick='update_twitter();'>

Share

May 14 2009

Google suffers major failure

Google Search and Google News performance slowed to a crawl, while an outage seemed to spread from Gmail to Google Maps and Google Reader. Comments about the failure were flying on Twitter, with “googlefail” quickly became one of the most searched terms on the popular micro-blogging site.


Share

May 14 2009

NYTimes Twitter Account Hacked

Another day, another high-profile Twitter account gets hacked. This time, it’s one of (many) New York Times’ Twitter accounts, The Moment, which brings news from their fashion blog of the same name.


Share

May 14 2009

Twitter Robot from 1935 (PIC)

Aug. 1935—Known as the “notificator,” the new machine is installed in streets, stores railroad stations or other public place where individuals may leave messages for friends. The machine is similar in appearance to a candy-vending device.


Share

May 12 2009

Twitter Passes NYT, WSJ in Unique Visitors

Where do you get your news from? While there’s a lot of reasons to visit Twitter online, it’s essentially a place to learn about what’s going on in your world. For the first time last month, the site saw more unique visitors than the websites of both the New York Times and the Wall St. Journal.


Share

May 12 2009

Mapping Hidden Twitter Data For Epidemiology

jamie found this visualization of air travel, which might be usable in some sort of proxy for the spread of flu virus (to choose a random application). Jer Thorp, an artist and educator from Vancouver, Canada (and a former geneticist), searched Twitter for the phrase “Just landed in” and obtained lat/lon coordinates for both the indicated airport and the Twitter user’s home location, as recorded in their Twitter profile. He them produced videos of multi-hour stretches of air travel that had been latent in the Twitter information stream.

Read more of this story at Slashdot.


Share

May 11 2009

O'Brien defence begins cross examination of Kilrea – Ottawa Citizen


CTV.ca

O'Brien defence begins cross examination of Kilrea
Ottawa Citizen
By Glen McGregor, The Ottawa CitizenMay 11, 2009 5:44 PM OTTAWA – A lawyer acting for Ottawa Mayor Larry O'Brien spent most of Monday cross-examining former mayoralty candidate Terry Kilrea over his history of repeated attempts to win public office
Kilrea tesitifies prominent Conservatives wanted a 'business Metro Canada – Ottawa
Ottawa mayor offered opponent money, federal job, court hears CBC.ca
580 CFRA Radio – National Post – Blackberry Cool – United Press International
all 109 news articles  Langue : Français
Share

May 11 2009

Twitter Meets Google Street View: Stweet!

We’ve seen plenty of interesting Google Maps mashups over the years, and Twitter mashups too; but rather than just plotting Tweets on a map, why not go even further and show the street Tweets are posted from?


Share

May 8 2009

Apple Reconsiders, Approves NIN iPhone App

gyrogeerloose writes “According to MacRumors, NIN’s iPhone application has been approved. Trent Reznor has reported via his Twitter account that the now-approved app was resubmitted without modification, which suggests that Apple reconsidered their initial rejection. This should really come as no surprise to anyone who follows Apple news since it follows the company’s typical pattern of handing potentially controversial iPhone apps, especially when it concerns high-profile rejections.”

Read more of this story at Slashdot.


Share

May 7 2009

100 Terrific Twitter Feeds for Young Entrepreneurs

Twitter is an excellent resource for learning, especially those who are building a business. You can learn about marketing, news, and more, and see what other successful young entrepreneurs are up to. Check out this list to find 100 feeds young entrepreneurs should watch on Twitter.


Share