post to Twitter with PHP and cURL


Using this scripts you can send messages to Twitter
from your web  site in a second. All you need to do is:
make sure that cURL is enabled
assign values for  user name and Twitter password in
 file  insertTwitterMsg.php (see the source code).
put files insertTwitterMsg.php and twitterAPI.php in
 the same folder  in web root folder like htdocs or public_htm
open file insertTwitterMsg.php in your browser fill in the text box
 and press button and that’s it.

<?php /* ---------------------------------------- */ // Change these parameters with your Twitter // user name and Twitter password. /* ---------------------------------------- */ $twitter_username =''; $twitter_psw =''; /* ---------------------------------------- */   // Don't change the code below /* ---------------------------------------- */ 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); } } /* ---------------------------------------- */ ?>   <html> <head> <title>Send a message to Twitter using PHP</title> </head>   <body> <h2>Post a message on Twitter</h2> <p>This page use Twitter API to send a message with postToTwitter() function.</p> <!-- This is the form that you can reuse in your site --> <?php if(isset($_POST['twitter_msg']) && !isset($error)){?> <div><?php echo $twitter_status ?></div> <?php } else if(isset($error)){?> <div>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> <!-- END --> </div> </body> </html>
<?php
function postToTwitter($username,$password,$message){
 
    $host = "http://twitter.com/statuses/update.xml?
status=".urlencode(stripslashes(urldecode($message)));
 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $host);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_POST, 1);
 
    $result = curl_exec($ch);
    // Look at the returned header
    $resultArray = curl_getinfo($ch);
 
    curl_close($ch);
 
    if($resultArray['http_code'] == "200"){
         $twitter_status='Your message has been sent!
<a href="http://twitter.com/'.$username.'">See your profile</a>';
    } else {
         $twitter_status="Error posting to Twitter. Retry";
    }
	return $twitter_status;
}
?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s