How to Tweet Using PHP without any Twitter App!

By Angsuman Chakraborty, Gaea News Network
Tuesday, January 13, 2009

Do you want to upgrade your site with twitter so that you can upgrade your messages each time through the site only? What big deal about that- You may ask. Well how about doing it without doing it without downloading any application! How about writing a php code which will help you to upgrade your tweets simply? Here is how.

Code

$username = 'myUserName';
$password = 'myPassword';
$status = '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);
}

Note:

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

So this is it. Additionally you can add some more Javascripts that counts the amount of characters in the Tweet field and such stuff.

Happy twittering.

[Source: brownphp.com]

Discussion

Anti-Spammer
July 22, 2010: 4:19 pm

Why don’t you just point to the original post and script instead of passing it off as your own you stinking spammer. By the way, I’m just a guy on the internet that hates this kind of crap. The brown guys are #1 in the results so that’s right in my book.

May 22, 2010: 3:33 pm

Hey,
I want to change the ‘from name’
Now twitter shows ‘from api’ but i want my own website as referal. like from marcelpronk.eu as example, just like from: twaiter!

Anybody can help?

TNX !

May 21, 2010: 12:31 am

I hope it will help me out

April 15, 2010: 6:44 pm

This is a great code snippet to get tweeting fast.

Since you’re transmitting sensitive data (a password!) you should opt to use the SSL protected URL
$tweetUrl = ‘https://twitter.com/statuses/update.xml’;

April 8, 2010: 7:15 pm

I dont understand article. anyone help me out a bit

July 16, 2009: 12:36 pm

The tweeting script worked a treat! many thanks.

I made small modifications and the rendering on the page made a bit of a mockery of the left and right quote character.

I modified so that all fields were $_POST['fieldname']; and $status = stripcslashes($_POST['blah']);
so now I carry a little html page on a pen drive with a input type=hidden name=blah value=blah> etc.

So I tweet pretty securely right off a pen drive which remains with me.

BobH.

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :