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_html
- open file insertTwitterMsg.php in your browser fill in the text box and press button and that’s it.
Feel free to copy source code of both files below or download twitter PHP scripts. You must take care not to send messages to often. To check the remaining tweets for the hour type in browser https://twitter.com/account/rate_limit_status.xml. Pay attention to nodes remaining-hits and hourly-limit. those will tel you limitations for your IP address. Keep in mind if you send link it will be converted to something like this http://bit.ly/8ljgJ. It is normal when sending links over API.
File name: insertTwitterMsg.php
<?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>
File name: twitterAPI.php
<?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; } ?>













[...] looks like this when using GET protocol. The template for using API calls can be found at http://php-mysql.develop.sitefrost.com/blog/2009/08/21/post-to-twitter-with-php-and-curl [...]