Tag Archiv: shorturl

PHP script for sending RSS feed to Twitter

I found a small sample PHP script for sending your RSS feed to Twitter. You can view / copy the code here:

<?php
/*
* Script that posts multiple RSS feeds to twitter
*
* Use this with crontab (it works with CLI too)
* to post your feed’s content to twitter
*
*/

$settings [‘twitter-username’] = ‘USERNAME’;// put here the twitter username you want to auto feed
$settings [‘twitter-password’] = ‘PASSWORD’;// the twitter password that belongs to the username
$settings [‘feed-url’] =‘FEED’;// here you place the url of the RSS feed you want to post to twitter

set_time_limit(0);//We need this because otherwise php will probably time out
//Let’s see if we have the curl library
if (!extension_loaded(‘curl’) && !dl(‘curl.’ . (stristr(php_OS, ‘WIN’)?‘dll’:‘so’))) die( ‘Curl is not loaded’ );
//Now we need a file to log the last entry so we wont post it again
$file = dirname(__FILE__) . ‘/feed.log.txt’;
if ( !
file_exists( $file ) ){
$fp = @fopen( $file, ‘w’);
if (! $fp )
die( ‘Could not write to log file. Check your permissions.’ );
fclose( $fp );
} else
if ( !is_writable( $file ) ) die( ‘Could not write to log file. Check your permissions.’ );
//Fetch the RSS feed
$ch = curl_init( $settings[‘feed-url’] );
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$feed = simplexml_load_string ( curl_exec($ch) );
curl_close($ch);
//Parse the feed
$messages = array();//Here we will store all the messages that we are going to post to twitter
foreach( $feed->channel->item as $item ){
$title = $item->title; // If you want to fetch the description instead use $item->description
$url = $item->link;
//Have we already posted that?
if ( $url == file_get_contents($file) ) break;
//Now do the actual posting.
//First we need to shorten the url
$ch = curl_init( ‘http://twt.gs/?url=’.$url.‘&user=’.$settings[‘twitter-username’].‘&pass=’.$settings[‘twitter-password’] );
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$shortURL = curl_exec($ch);
$resp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ( $resp != ‘200’ )
die(‘Problem with the url shortening service. Check back later. Error: ‘.$shortURL);
//Now Calculate the twit message
$cnt = 140 strlen($shortURL) – 1;
$messages [] = strlen($title) <= $cnt ? $title . ‘ ‘ . $shortURL : substr($title,0,$cnt3).‘… ‘.$shortURL;
$lastURL = empty($lastURL) ? $url : $lastURL;
}
//Post to twitter
while( $message = array_pop($messages) ){
$ch = curl_init(‘http://twitter.com/statuses/update.xml’);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ‘status=’.urlencode($message));
curl_setopt($ch, CURLOPT_USERPWD, $settings[‘twitter-username’].‘:’.$settings[‘twitter-password’]);

$response = curl_exec($ch);
$resp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ( $resp != ‘200’ )
die(‘Problem with twitter. We should try later. Twitter reported: ‘.$response);
else
sleep(5);//Sleep 5 seconds before the next update
}
$fp = fopen($file, ‘w’);
fwrite($fp, $lastURL);
fclose($fp);
?>

What the above script is missing is the auto starting. You can use an external cronjob service if your hosting provider does not give you access to CRONjobs.

Powered by Gewgley