Getting a [function.file-get-contents]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Vaughanjb
Forum Commoner
Posts: 26
Joined: Thu Jul 08, 2010 4:56 am

Getting a [function.file-get-contents]

Post by Vaughanjb »

I'm using twitter to get the latest post and randomly get this warning every now and then.

Warning: file_get_contents(http://twitter.com/statuses/user_timeli ... ng&count=1) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home/content/v/a/u/vaughanbarwood/html/bt_web/index.php on line 5

Warning: Variable passed to each() is not an array or object in /home/content/v/a/u/vaughanbarwood/html/bt_web/index.php on line 25
This is my code :

Code: Select all

<?
    $username = "briansailing";
    $limit = 1;
    $feed = 'http://twitter.com/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] = 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 = $v;
}
?>
Then I just echo it out later on, I don't really know why I'm getting the error. Does anyone know if I get a warning, if I could just cancel the script and leave the section blank?

Any help would be great.

Thanks

V
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Getting a [function.file-get-contents]

Post by VladSun »

Code: Select all

$username = "briansailing";
$limit = 1;
$feed = 'http://twitter.com/statuses/user_timeline.rss?screen_name='.$username.'&count='.$limit."";
$tweets = file_get_contents($feed);
var_dump($tweets);
Strange, it works for me ...
Is it the *exact* code you are using?
There are 10 types of people in this world, those who understand binary and those who don't
Vaughanjb
Forum Commoner
Posts: 26
Joined: Thu Jul 08, 2010 4:56 am

Re: Getting a [function.file-get-contents]

Post by Vaughanjb »

It works 80% of the time, then random it will change and I will get that warning for about 10 to 15 mins, then it will go back to normal.
Post Reply