Getting a HTTP 400 Error


#1

Hi

I’ve tried to implement a basic twitter display on a few websites, using the following code:

<?php
    $username = "iamtheste";
    $limit = 1;
    $feed = 'http://twitter.com/statuses/user_timeline.rss?screen_name='.$username.'&count='.$limit;
	
    if ($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 .= "<div id='tweet'>$v</div>\n";
		}
	} else {
		$tweetout = "<div id='tweet'>Our Twitter feed seems to be having trouble at the moment. You can visit our Twitter page <a href='' target='_blank'>here</a>.</div>";
	}
?>

And basically it’s returning a HTTP 400 Bad Request error. Now, I’m really not much of a developer at all and I’ve basically managed deduce from reading a few posts on here etc that Twitter limits the amount of requests per hour or something along those lines. Now this ‘Twitter Feed Display’ is on two websites (both sat on the same server - which is shared hosting, which limits what I can do on there somewhat). I’m basically look for the reason it’s throwing up errors and also what I can do to fix it? Any help would be appreciated

Thank you

*EDIT I should point out, I’ve seen the Twitter Widget that allows you to display tweets. But I was ideally after something a could style/design more. Essentially creating my own widget if you like.


#2

I recommend using a proper API URL for the RSS feed – instead of

$feed = ‘http://twitter.com/statuses/user_timeline.rss?screen_name=’.$username.’&count=’.$limit;

use something more like:

$feed = ‘https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=’.$username.’&count=’.$limit;

400 Bad Request indeed usually means you’re being rate limited. This request is unauthenticated, and a single IP address can only make 150 unauthenticated requests in an hour – since you’re asking for the feed server-side, it would be wise for you to only pull the RSS feed occasionally, decoupling the API call from the viewing of the page that will display the tweets – the API isn’t meant to be used per page view.


#3

Thanks for the reply.

So if I change where I pull the feed from (as you suggested) I’m guessing I am still going to be rate limited to 150?
If so, what would you suggest would be the best method for getting around this?


#4

Is this code getting obsolete in march 2013??

Thanks my feed is working again!


#5

Yes, these old widgets will be completely unfunctional come March 2013. The best way to avoid that happening is to use [node:10248] instead.


#6

Help for setting please. Thanks… :slight_smile:


#7

problem or are simply searching for data on Eurostat’s website you can also call the following telephone-- , .


#8

#9