rss2twitter.php legacy source code
This is the old code. Due to recent Twitter updates, it won't work just the way it is any more, as
basic authentication is no longer supported by the Twitter API.
The all new updated version using OAuth can be found here though.
<?php
/*
RSS to Twitter v0.1
by paul stamatiou
of http://paulstamatiou.com
based on code from
http://morethanseven.net/posts/posting-to-twitter-using-php
Updated by mikey beck 17/08/10
*/
include('parse.php'); //parse.php a.k.a. lastRSS 0.9.1 should be supplied with this file.
$uname = 'yourtwitterusername';//example "blah" for twitter.com/blah, or your email address
$pwd = 'yourtwitterpassword';
$twitter_url = 'http://twitter.com/statuses/update.xml';
$feed = "yoursyndicationfeed"; //the feed you want to micro-syndicate
$rss = new lastRSS;
$url = "yoururl"; // delete this line if you intend to use tinyURL
if ($rs = $rss->get($feed)){
$title = $rs[items][0][title];
// total length of tweet is 140chars. The +7 at the end is for
// the extra characters that are always used (spaces, dots etc)
$tweetlen = 140 - (strlen($title) + strlen($url) + 7);
//body of tweet as long as possible
$tweetbody = substr($rs[items][0][description], 0, $tweetlen);
//$url = $rs[items][0][link]; // uncomment this line if you want to use tinyURL
} else { die('Error: RSS file not found, dude.'); }
//also uncomment the line below if you want to use tinyURL
//$tiny_url = file_get_contents("http://tinyurl.com/api-create.php?url=" . $url);
//...and replace '$url' with '$tinyURL'
$status = $title . " - " . $tweetbody . "... " . $url;
echo $status; //just for status if you are directly viewing the script
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_URL,"$twitter_url");
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle,CURLOPT_POST,1);
curl_setopt($curl_handle,CURLOPT_POSTFIELDS,"status=$status");
curl_setopt($curl_handle,CURLOPT_USERPWD,"$uname:$pwd");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if (empty($buffer)){echo '<br/>message';}else{echo '<br/>success';}
?>
