Here's something:
PHP Code:
<?php
$username = 'username';
$password = 'password';
$filename = '/path/to/textfile';
$script_home = '/path/to/script/dir';
$desc = 'file has changed:';
$show_contents = true;
// Don't change anything down here
function twitter_update($username, $password, $message) {
$c = curl_init();
$message = str_replace(' ', '+', $message);
$message = str_replace("\n", '+', $message);
curl_setopt($c, CURLOPT_TRANSFERTEXT, true);
curl_setopt($c, CURLOPT_URL, 'http://twitter.com/statuses/update.json');
curl_setopt($c, CURLOPT_USERPWD, $username.':'.$password);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, 'status='.$message);
curl_exec($c);
curl_close($c);
}
if (!file_exists($script_home.'/datafile') || (md5_file($script_home.'/datafile') != md5_file($filename))) {
if (!copy($filename, $script_home.'/datafile')) {
die('Unable to copy file for local storage; '.$script_home.' needs world-writeable permissions');
}
$data = $desc;
if ($show_contents) {
$readlen = 140 - strlen($desc) - 1;
$rsrc = fopen($script_home.'/datafile', 'r');
if ($rsrc === false) die('Unable to open file; check permissions?');
echo 'reading';
$innards = fread($rsrc, $readlen+1);
if (strlen($innards) > $readlen) {
$innards = substr($innards, 0, -4);
$innards .= '...';
}
fclose($rsrc);
$data .= ' '.$innards;
echo $data;
}
twitter_update($username, $password, $data);
}
?>
Explanation of variables:
$username - Twitter username
$password - Twitter password
$filename - full path to text file (e.g. /home/worldwise001/mytext
$script_home - full path to directory with script (e.g. /home/worldwise001/myscript)
$desc - message text to display on twitter
$show_contents - show the (partial) contents of the file (if true)
Directions:- Copy and modify the contents of the above code snippet into a php file (e.g. tweet.php)
- Upload the file to the script directory, e.g. to /home/worldwise001/myscript/tweet.php
- chmod the script directory to 0777
- Add a cron job in your cPanel to run the script, e.g.:
Code:
php -f /home/worldwise001/myscript/tweet.php
Hope this helps.