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:

  1. make sure that cURL is enabled
  2. assign values for user name and Twitter password in file insertTwitterMsg.php (see the source code).
  3. put files insertTwitterMsg.php and twitterAPI.php in the same folder in web root folder like htdocs or public_html
  4. 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;
}
?>

Related Posts

  1. Twitter Rate limit exceeded
  2. Post to Twitter download page
  3. Twitter search

13 Responses to “post to Twitter with PHP and cURL”

  1. [...] 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 [...]

  2. Jorgen says:

    Nice article. If your tweet contains a URL, an improvement could be to include bit.ly URL shortening, and some kind of content shortening. You can find a example on how to do this here: http://tips4php.net/2010/03/auto-publish-on-twitter/

  3. Scenografia says:

    I also have a web site and i want to know, where can i download theme similar to yours?

    • Mike says:

      This theme is improved with widgets and plugins.
      In the right side-bar I placed three widgets: categories, recent posts and HTML widget. In HTML widget I placed a FeedBurner counter, you need to have an account at that site.
      In the left side-bar I added Facebook widget and Twitter button. The web page for the theme is at http://dansette.com/2009/04/free-wordpress-theme-corp/.

  4. hey, above information is very helpful and so easy to implement. I was so worried before looking at this article, but things are done much easily and quickly than expected :)

    thanks a lot !

    Keep up some good postings.

    Cheers,

    Team | Joomla Outsource India

  5. cvul says:

    This code also works for another Twitter like API system. Thanks for sharing.

  6. karuppasamy says:

    i’m also use this code but in my twitter account it says like “less than 20 seconds ago via API”. can i change the Api to mysite name? is it possible? please tell your suggestion.

    Thanks in advance

    • Mike says:

      API stand for Applictaion Programing Interface. You can try some WordPress plugin (I don’t know which one sorry) or hire a developer to create an application called by your site name.

  7. Lil says:

    This solution uses Basic Authentication, which Twitter will no longer support after June 2010. For a supported “Oauth” library in PHP, see Abraham Williams’ PHP Twitter library.

    • Mike says:

      That deadline is extended to 25th. august. I’ll rewrite articles so that OAuth replaces Basic Authentication.

  8. It’s nice to see an interesting article about Twitter plus social networking every now and again. Thanks for an excellent read!

Leave a Reply

Dansette
SEO Powered by Platinum SEO from Techblissonline