Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This works, however I keep getting a failed to open stream every so many random refreshes/page views. I've also tried this code on two different hosts with the same results. I've also tried using cURL with the same results. It keeps showing an error on the file_get_contents. Is there a conflict with wordpress and file_get_contents?

function get_tweets($twitter_username, $tweet_count) {
if(($twitter_info = file_get_contents('https://api.twitter.com/1/users/show.json?screen_name=' . $twitter_username)) === FALSE) {
    return false;
}
$twitter = array();//store all info in this array
if(($profile_info = @json_decode($twitter_info, true)) === FALSE) {
    return false;
}
$twitter['name'] = $profile_info['name'];

$tweets = array();

if(($tweets_json = file_get_contents('https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name='.$twitter_username.'&count='.$tweet_count))) {
    if(($all_tweets = @json_decode($tweets_json, true)) === FALSE) {
        return false;
    }
    foreach($all_tweets as $tw) {
        $tweets[] = array('tweet'=>make_clickable_urls($tw['text']), 'created_at'=> 'about '. time_since(strtotime($tw['created_at'])) . ' ago', 'profile_image_url'=>$twitter['profile_image_url']);
    }
}
else {
    return false;
}
$twitter['tweets'] = $tweets;
return $twitter;
}
share|improve this question

closed as off topic by Chip Bennett, s_ha_dum, toscho Jan 4 '13 at 21:02

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Close-voted as off-topic. This is a Twitter API rate-limiting issue, not a WordPress issue. You'll need to look into a Twitter API solution that incorporates oAuth. –  Chip Bennett Jan 4 '13 at 19:07
    
Well according to the response below, this is still relevant to Wordpress in the way of retrieving the data. –  JonnyPlow Jan 4 '13 at 20:39
    
Also, wouldn't the rate limit only kick in after so many loads? I'm reloading maybe 5 times and it errors, at maybe 30-40 times every hour. So I'm not hammering the system at all. –  JonnyPlow Jan 4 '13 at 21:08
    
Just because it happens in the context of WordPress does not mean that it is in-scope for WPSE. In this case, you know your WordPress implementation is correct, because when the API call is working, your WordPress implementation is working. The only thing that changes is the Twitter API response, which is the source of the "failed to open stream" error. You can change/improve the way you make the remote HTTP call, but if the Twitter API returns a bad response, the problem is still the Twitter API. –  Chip Bennett Jan 4 '13 at 21:35

1 Answer 1

How to do remote requests in WordPress: Use the appropriate API

First, there's the WP HTTP API, which should be used for such tasks. And second, one should never ever use the @ operator, as this one suppresses error messages or notices and will successfully lock you out from troubleshooting your bugs. Just because you can't see it, doesn't mean that it's not there.

How to use the API

The first thing to do is make the actual request:

// Example
$api_url  = https://api.twitter.com/1/users/show.json?screen_name=SOME_USER_NAME';
$response = wp_remote_get( $api_url );

Then you check if you got an error and output it for debugging.

if ( is_wp_error( $response ) )
    return $response->get_error_message();

The headers can give you additional information. The same goes for the response code and the response message

$headers = wp_remote_retrieve_headers( $response );
$code    = wp_remote_retrieve_response_code( $response );
$message = wp_remote_retrieve_response_message( $response );

Of course most of the response code and message is a lie and just what the server answers. Remote APIs are - in my personal experience - mostly lousy coded and send 200/OK answers for errors as well.

Now that you got everything through, you finally just grab the result and do whatever you need to do with it:

$data = wp_remote_retrieve_body( $response );
// BE FANTASTIC HERE!

How to debug WP HTTP requests

The one who implemented this API originally, was clever enough to offer a hook at the end of a request, that gives you every detailed information that you can imagine. Just use it.

add_action( 'http_api_debug', 'wpse78251_http_api_debug', 10, 5 );
function wpse78251_http_api_debug( $response, $type, $class, $args, $url ) {
    var_dump( 'Request URL: ' . var_export( $url, true ) );
    var_dump( 'Request Args: ' . var_export( $args, true ) );
    var_dump( 'Request Response : ' . var_export( $response, true ) );
}

More info about debugging those requests can be found on this answer I gave.

Additional Information / Advanced and in-depth info

I wrote another answer about remote requests for this question, which also shows the basic concept of caching and parsing the response.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.