I'm using PHP to display the most recent tweet from a user. This is in Wordpress. This works most of the time - but sometimes, I get this error:
file_get_contents(http://api.twitter.com/1/statuses/user_timeline/[username].json) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in [...]/twitter.php on line 47
I'm absolutely certain that I'm not going over the Twitter API limit, because even if my caching code is flawed, no one else can see this - it's hosted locally - and there's no way I viewed the page 150 times in an hour. I've tested that the username and database entries are indeed being retrieved. This is my code:
<?php
function twitter($username) {
$tweet = '';
echo $username;
if (!get_option('twitter_last_updated')) {
$format='json';
$tweet_raw=file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}");
$tweet = json_decode($tweet_raw);
add_option('twitter_last_updated', time(), "", "yes");
add_option('twitter_last_updated_author', $username, "", "yes");
add_option('twitter_last_updated_data', $tweet_raw, "", "yes");
} elseif (time() - get_option('twitter_last_updated') > 30 || get_option('twitter_last_updated_author') != $username) {
$format='json';
$tweet_raw=file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}");
$tweet = json_decode($tweet_raw);
update_option('twitter_last_updated', time());
update_option('twitter_last_updated_author', $username);
update_option('twitter_last_updated_data', $tweet_raw);
} else {
$tweet = json_decode(get_option('twitter_last_updated_data'));
} ?>
<!-- display the tweet -->
<?php } ?>
I would really appreciate some help with this. I feel totally stumped.