This blog has moved here: woorkup.com
Monday, October 29, 2007

Twitter: send message from a PHP page using Twitter API

Are you a Twitter addicted? This post illustrates how to post a message from a custom PHP page using the Twitter API

This is a very simple tutorial (really just some line of code!) that explains how to post a message using Twitter API from a PHP page.



The tutorial includes a folder called twitter with two PHP file:
  1. 1. insertTwitterMsg.php (it's the application interface)
  2. 2. twitterAPI.php (it's the Twitter API with some changes)

Download original script

Download improved script by Scott Sloan Updated January 08, 2008


Original script: what you have to modify?
The script is ready to use but first, in the file insertTwitterMsg.php you have to modify only two parameters: $twitter_username, with your Twitter username and $twitter_psw, with your Twitter password.

<?php
/* ---------------------------------------- */
// Change these parameters with your Twitter
// user name and Twitter password.
/* ---------------------------------------- */
$twitter_username ='yourTwitterUserName';
$twitter_psw ='yourTwitterPassword';
/* ---------------------------------------- */

/* Don't change the code belove
/* ---------------------------------------- */
require('twitterAPI.php');
if(isset($_POST['twitter_msg'])){
$twitter_message=$_POST['twitter_msg'];
if(strlen($twitter_message)<1){ $error=1; } else { $twitter_status=postToTwitter($twitter_username, $twitter_psw, $twitter_message); } }
/* ---------------------------------------- */
?>


Don't touch the rest of the code!

In insertTwitterMsg.php you have a form that you can reuse in your web projects:


<!-- Send message to Twitter -->
<?php
if(isset($_POST['twitter_msg']) && !isset($error)){?>
<div class="msg"><?php echo $twitter_status ?></div>
<?php } else if(isset($error)){?>
<div class="msg">Error: please insert a message!</div>
<?php }?>
<p><strong>What are you doing?</strong></p>
<form action="insertTwitterMsg.php" method="post">
<input name="twitter_msg" type="text" id="twitter_msg" size="40" maxlength="140"/>
<input type="submit" name="button" id="button" value="post" />
</form>


Save the folder with the tutorial in your localhost, remember to set the correct parameters and launch insertTwitterMsg.php with your browser. Now, you are ready to post messages on Twitter from your PHP page :)

For other info about the modified script provided by Scott Sloan, take a look here


Related posts
- Simple PHP Twitter Search ready to use in your web projects
- Super simple way to work with Twitter API (PHP + CSS)
- Send messages from a PHP page using Twitter API

blog comments powered by Disqus
Martin said...

Hi! Did you know if there's a way to "read" and parse twitter responser over a particular user?

Anonymous said...

I am not sure how the improved script is improved since it doesn't seem to work. I am sticking with the original script since it works.

Anonymous said...

worst indentation ever? :P

Anonymous said...

sweetter

Kevin said...

I guess use of CURL make programming easy in php... i m planning to use it...

Anonymous said...

do you have a demo of a site that uses this script?

thanks

Anonymous said...

Thanks for this nice post, helped me get started

Matt said...

How do I make it so on twitter it says the update came from my website instead of "the web"

Caneco said...

@Mark

You have to ask twitter for that

twitter request

I don't know it's how to put the parameter in there for this to work.
Anyone?

Dr. Chuck said...

Thank you so much. I know this seems simple - but you made this so simple and easy.

Kerry said...

If you're using the improved script, when updating your status more than once within a single page (ie if you wish to say "just woke up" and "is having the day off" one after another) I found the second status on twitter would be:
just woke upstatuses/update.xml?status=is having the day off

To remedy this, in the twitter.php remove the line:
private $twitterHost = "http://twitter.com/";

and add in the line:
$this->twitterHost = "http://twitter.com/";
beneath:
public function setStatus($stat) {

Besides that, well done.

frank said...

Hi, is it possible to change this script so he can read a songtitle from a shoutcast server/or a variable from a php file, and post a that result on twitter?


thanks,
frank

Anonymous said...

hi there, can you guys post a zipped file of the original script on your server, as this link is blocked: http://www.box.net/shared/trnomoyh15

or if you can post the code of the files so i can copy and paste it.

thanks

oneafrikan said...

Thanks! Great script, nice work ;-)

Marc de Ruijter said...

Hi, nice script, works great on muy local host with curl installed.

Is there a workaround for getting this to work at a machine without curl?

nico said...

I needed to add this line:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

Without it, I got an Expectation Failed error from twitter

SPIKE-That's my cooler nickname said...

hey scot,
today i saw a site which asks you to tweet it with your username/pass and they will automatically send you a free ebook to anyone who tweets you.

how do i make that app for my site? any ideas?

Twittrblog said...

@matt


I don't think they would allow you to put your website name in there api like that.. I mean Twitter doesn't even put "from twitter" but on second thought, how do all the other apps get permission?

UVL said...

Thanks, it worked at the fist test, thanks so much.

Simon said...

Thanks, needed this!
Although with your posting-page (actually, the posting-code), I run into a slight problem, but I'll fix it. The problem is on my side, not your code.

heng said...

thanks for your script!it is useful for me.

AndyGreenwood23 said...

Absolutely fantastic. Thank you.

Daily said...

I'm new to php and I just implemented the original script and sent a tweet with it within 10 mins. Haven't tested it beyond that - but pretty impressive so far. Thanks!

Anonymous said...

When I tried to implement the first code, but it is giving me constant error "Error posting to Twitter. Retry". I tried, to implement the second code, and it says successfully submitted, but does not show on my twitter page. can anyone suggest me modification?

Sunil said...

not sure why others having problems. Mine worked in the very first attempt !! great job !!!

rob said...

"today i saw a site which asks you to tweet it with your username/pass and they will automatically send you a free ebook to anyone who tweets you. how do i make that app for my site? any ideas?"sounds completely like phishing to me. i'd avoid giving your twitter password to any site that isn't an official twitter-affiliated site (or another big site that you know and trust that wants to collect relevant twitter data to update your account with them- although even the this is debatably a security risk).

rob ganly

Ranjith K Avarachan said...

30 seconds !!! yeah thats all the time i took to post a tweet from your script


thanks a lot

Robert said...

How can you post to a Fan Page?

Feroz said...

How can I add a link in my status

  • Twitter Follow woork on Twitter
  • RSS Feed Subscribe to stay up to date
  • Podcast Coming soon...
  • 0 delicious lovers save
Support this blog with a micro subscription
If you like this blog you can contribute with a micro subscription via PayPal.
Share your links. Do you want to suggest any interesting link about web design or tech news? Submit your link.
Submit a News
Web Templates
Stock Vectors
THE CSS AWARDS