Looking for simple and basic twitter plugin

R

ryanmaelhorn77

Guest
Looking for a script/plugin/whatever, that can tweet the contents of a txt file I have on my x10 site if it has changed from the last tweet. the txt file will be updated at irregular times by another program running on my pc. Just need something to check it at set times (every 12 hours perhaps?) see if it has changed, and tweet it out if it has.

That's it, no need to recieve msgs from twitter or anything else, simply check and post.

???
 

worldwise001

Member
Messages
57
Reaction score
1
Points
8
Here's something:

PHP:
<?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:
  1. Copy and modify the contents of the above code snippet into a php file (e.g. tweet.php)
  2. Upload the file to the script directory, e.g. to /home/worldwise001/myscript/tweet.php
  3. chmod the script directory to 0777
  4. Add a cron job in your cPanel to run the script, e.g.:
    Code:
    php -f /home/worldwise001/myscript/tweet.php

Hope this helps.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
There's going to be a little problem with that -- Twitter has dropped basic authentication for the API (at the end of July, in fact) and now requires oAuth. That means you now need to make an authentication token request to your oAuth provider, then provide the token in a separate request to Twitter in exchange for an authorization token that you can then provide as your credentials for the status update. No more simple user/password in the request.
 

worldwise001

Member
Messages
57
Reaction score
1
Points
8
Hmm... are you sure about that? It worked for me last night when I tested it (I wrote this from scratch).
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
Support for basic authentication was set to be disabled on the 16th of this month, so it seems odd that it is still working. Either way though, it is a dead method for any current development.
 
Top