9, November 2009

Webdigity tutorials : Post an RSS feed to twitter

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Tutorials @ webDigity
Post an RSS feed to twitter

Subscribe to our tutorials feed!

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($chCURLOPT_TIMEOUT10);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
$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($chCURLOPT_TIMEOUT10);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
$shortURL curl_exec($ch);
$resp curl_getinfo($chCURLINFO_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($chCURLOPT_CONNECTTIMEOUT10);
curl_setopt($chCURLOPT_RETURNTRANSFER1);
curl_setopt($chCURLOPT_POST1);
curl_setopt($chCURLOPT_POSTFIELDS'status='.urlencode($message));
curl_setopt($chCURLOPT_USERPWD$settings['twitter-username'].':'.$settings['twitter-password']);

$response curl_exec($ch);
$resp curl_getinfo($chCURLINFO_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);
?>


Like this tutorial? Feel free to retweet it :)
Do you like this tutorial?
Feel free to share it with others using Dzone or your favorite bookmarking service. You can see related discussions in our php forum
This tutorial posted by Nikolas from Sublime Directory

Sponsors :

Code library menu :
[ Tutorials home | Submit a tutorial ]

User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Nov 09, 2009, 07:07:04 am





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 42.741
Total Topics: 8.375
Total Members: 6.939
Tutorials : 58
Resources : 929
Designs : 341
Latest Member: Auguste Dubuisson

19 Guests, 3 Users online :

15 users online today:




Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2009 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.