A php script that you can use to post your blog's feed to twitter. The script will convert urls using the twt.gs url shortening service, and it will post it to twitter along with the post's title.
Ok, so you finally got into the twitter train, and want to send some updates from your blog/web site. There are several services that do exactly that, but most of them seem to lag or send many updates once per day which is definitely annoying to your followers. The bellow script will work as is, so the only thing you have to do to make it work is to change it's settings (the $settings array)
The code is self explained in comments, but if you have any questions feel free to post in our php forum.
Code:
<?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 * * A tutorial by: * http://www.webdigity.com/ * */
$settings ['twitter-username'] = 'USERNAME';// Your twitter username $settings ['twitter-password'] = 'PASSWORD';// Your twitter password $settings ['feed-url'] ='FEED';//The url of the feed we are going 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,$cnt-3).'... '.$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); ?>