I am using PHP to show the newest tweet from the user. This really is in Wordpress. This works more often than not - but sometimes, I recieve this error:

file_get_contents(http://api.twitter.com/1/statuses/user_timeline/[username].json) [function.file-get-contents]: unsuccessful to spread out stream: HTTP request unsuccessful! HTTP/1.1 400 Bad Request in [...]/twitter.php online 47

I am very sure that I am not groing through the Twitter API limit, because even when my caching code is problematic, nobody else can easily see this - it's located in your area - and there is not a way I seen the page 150 occasions within an hour. I have examined the username and database records truly are being retrieved. This really 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'd really appreciate outside assistance with this particular. Personally i think totally stumped.

How frequently are you currently calling the function? Basically remember properly, twitter lately transformed all the calls each hour from 150~ to 75 each hour. You might like to cache the outcomes, so they won't consume your financial budget.

Check this out slashdot story: Twitter Throttling hits 3rd party apps

First, you shouldn't be utilising file_get_contents to retrieve "files" within the network. You need to use curl. It may be just system response delays, or twitter giving a redirect for load balancing. file_get_contents does not follow redirects and essentially occasions out immediately. Curl could be set to follow along with redirects and adheres towards the network timeout (one minute I believe) if virtually no time out is specified. Most significantly, curl will easily notice why it unsuccessful.

The reason for not while using WordPress HTTP API? This is just what it had been created for - a wrapper for dealing with HTTP using standard WordPress functions, no matter platform or set-up.

I authored a something similar to that which you have also it keeps failing like all 3 demands, the answer was increase your little cache system and @'s around the file_get_contents to prevent php from tossing errors to customers.

When twitter fails, and it'll fail a great deal, you simply fetch data from that formerly built cache.

I additionally don't recommend you adding this onfly, it could decelerate the entire page building due twitter issues.