views:

47

answers:

4

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.

+1  A: 

How often are you calling the function? If I remember correctly, twitter recently changed the maximum amount of calls per hour from 150~ to 75 per hour. You might want to cache the results, so as not to use up your allowance.

See this slashdot story: Twitter Throttling hits 3rd party apps

Blue Peppers
Yep, I've been bit by this as well. My version uses client-side javascript, but while debugging other issues I noticed my twitter feeds were down, and it took me a while to figure out why...
Chris Arguin
I doubt I'm calling it more than 20 times an hour, as I said, it's just me.
Debashis
+1  A: 

First, you should not be using file_get_contents to retrieve "files" over the network. You should use curl. It could be just system response delays, or twitter issuing a redirect for load balancing. file_get_contents doesn't follow redirects and basically times out immediately. Curl can be set to follow redirects and adheres to the network timeout (1 minute I think) if no time out is specified. Most importantly, curl can tell why it failed.

Brent Baisley
Is Curl installed with PHP by default? You see, I'm making a premium Wordpress theme, so I need to be sure anything I use is going to be installed on any server a customer uses.
Debashis
I've yet to find a place without cURL, and remote `file_get_contents` is disabled on some hosts (particularly shared ones).
ceejayoz
Thanks a lot, I got it working with cURL.
Debashis
A: 

Why are you not using the WordPress HTTP API? This is exactly what it was designed for - a wrapper for working with HTTP using standard WordPress functions, regardless of platform or set-up.

TheDeadMedic
A: 

I wrote a something like what you have and it keeps failing like every 3 requests, the solution was build up a little cache system and @'s on the file_get_contents to avoid php from throwing errors back to users.

When twitter fails, and it will fail a lot, you just fetch data from that previously built cache.

I also don't recommend you adding this onfly, it might slow down the whole page building due twitter issues.

Rodrigo