Twitter (randomly getting Bad Request)

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

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

Twitter (randomly getting Bad Request)

Post by Vaughanjb »

I have used PHP to get the latest tweet from a persons twitter. It is working most times but randomly it gives me an error saying bad request after changing nothing. Then after a while it will return to normal.

Code: Select all

<?
    $username = "vaughanbarwood";
    $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 it is used like this.

Code: Select all

<? echo $tweetout ?>
The lines the error points to are line 5 and line 25

I can't see any reason it would be breaking randomly the only thing I can think of to get around it, is to stop the script if it gets an error? Maybe?

Any help would be great and if I correct how would I go about stopping the script.

Thanks

Vaughanjb
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Twitter (randomly getting Bad Request)

Post by Benjamin »

:arrow: Duplicate = Locked
Locked