Link to home
Start Free TrialLog in
Avatar of Dean OBrien
Dean OBrienFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Twitter feed for website

Experts,

Over the last month or so, i have been having problems with twitter feeds on a few websites.

Can anyone explain exactly what limits are imposed on a website by twitter, before it stops allowing a feed? I have the exact same files located in two different locations, and the feed only works in one for the time being.

http://www.suremedia.co.uk/craftbeer/    (works)
http://www.craftbeerrising.co.uk/    (doesnt work)

The php code i am using is:
<?
    $username = "craftbeerriseuk";	// your twitter username here!
    $limit = 2;
    //$feed = 'http://twitter.com/statuses/user_timeline.rss?screen_name='.$username.'&count='.$limit;
    // UPDATE 10-17-2012: change in the Twitter API
    $feed = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$username.'&count='.$limit;
    $tweets = file_get_contents($feed);
    
		$tweets = str_replace("&", "&", $tweets);	
		$tweets = str_replace("<", "<", $tweets);
		$tweets = str_replace(">", ">", $tweets);
		$tweet = explode("<item>", $tweets);
    $tcount = count($tweet) - 1;

for ($i = 1; $i <= $tcount; $i++) {
    $endtweet = explode("</item>", $tweet[$i]);
    $title = explode("<title>", $endtweet[0]);
    $content = explode("</title>", $title[1]);
		$content[0] = str_replace("&#8211;", "&mdash;", $content[0]);
		$content[0] = str_replace("&amp;amp;", "&", $content[0]);
	
		$content[0] = preg_replace("/(http:\/\/|(www\.))(([^\s<]{4,68})[^\s<]*)/", '<a href="http://$2$3" target="_blank">$1$2$4</a>', $content[0]);
		$content[0] = str_replace("$username: ", "", $content[0]);
		$content[0] = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $content[0]);
		$content[0] = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $content[0]);
    $mytweets[] = $content[0];
}

while (list(, $v) = each($mytweets)) {
	$tweetout .= "<div>$v</div>\n";
}
?>
<?=$tweetout;?>

Open in new window


I notice there has beenan update to the twitter API which is obviously causing the problems, but the behaviour since has been very frustrating...

Can anyone suggest a solid method to bring a feed into a site (using a simple block of php code) that works every time? or will we always be at the mercy of fair use limits when testing>?

Regards
Easynow
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Dean OBrien
Dean OBrien
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dean OBrien

ASKER

Thanks again