Monday, 29 October 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

No comments:

Post a Comment