Author Topic: Automatic Twitter status update - tweet script or cron job  (Read 5443 times)

Rimy

  • Guest
Hi, I need a script that would run as a cron job every 30-40 minutes and post automatic tweets  about 20-40 different default tweets so that it   can repeat. Can anyone here create such script? Thanks!

phphelp

  • Administrator
  • Senior Member
  • *****
  • Posts: 868
  • Karma: 3
    • View Profile
    • PHP Help forum
Re: Automatic Twitter status update - tweet script or cron job
« Reply #1 on: May 26, 2010, 04:55:34 AM »
Hi there. Here is the function that you can use to submit tweets to your twitter account:

PHP Code: [Select]

<?php
function tweet($message$username$password) {
  
$context stream_context_create(array(
    
'http' => array(
      
'method'  => 'POST',
      
'header'  => sprintf("Authorization: Basic %s\r\n"base64_encode($username.':'.$password)).
                   
"Content-type: application/x-www-form-urlencoded\r\n",
      
'content' => http_build_query(array('status' => $message)),
      
'timeout' => 5,
    ),
  ));
  
$ret file_get_contents('http://twitter.com/statuses/update.xml'false$context);
 
  return 
false !== $ret;
}
?>


To use this function, create file named for example post_tweet_to_twitter.php
In that file place the code above and call the function as shown below:

PHP Code: [Select]

<?php
  tweet
('Tweet tweet tweet...''your_twitter_account''your_passw');
?>


You can add a task to your cron manager and set it up to run on required intervals. The command would be like shown below (using Lynx browser):

Code: [Select]
lynx -source http://www.yourwebsite.com/post_tweet_to_twitter.php > /dev/null
Are you experienced in PHP programming and willing to share your knowledge?
Join us at php help forum!