Usertimeline request very slow response, API slow?


#1

I am retrieving tweets from multiple accounts (around 20) and displaying them on a page. The request are very slow and my page takes one to two minutes to load. I am using the twitteroauth library (PHP).

Here’s the function

    //twitter credentials and connection
    $consumer_key = variable_get('tw_consumer_key', 'XXXXX); //consumer key
    $consumer_secret = variable_get('tw_consumer_secret', 'XXXXXX'); // consumer secret
    $oauth_token = variable_get('tw_access_token', 'xxxxxxx'); //oAuth Token
    $oauth_token_secret = variable_get('tw_access_token_secret', 'xxxxxxxxx'); //oAuth Token Secret

    $connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
    $connection->host = "https://api.twitter.com/1.1/";


    //Retrieve feeds now
    foreach ($twitter_accounts as $account_twitter) {
        if (!empty($account_twitter['lien'])) {
            $page_url = $account_twitter['lien'];
            $twitter_name = $account_twitter['compte'];

            $query = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' . $twitter_name . '&exclude_replies=true&include_rts=true&include_entities=true';

            $content = $connection->get($query);

            if (sizeof($content) > 0 && empty($content->errors)) {
                $tw_tweets['posts'] = $content;
                $tw_tweets['url'] = $page_url;
                $twitter_feeds[] = $tw_tweets;
            }//end if sizeof
            else {
                if (!empty($content->errors)) {
                    $error = '';
                    $error = (isset($content->errors[0]->message)) ? $content->errors[0]->message : '';
                    $error .= (isset($content->errors[0]->code)) ? ' code' . $content->errors[0]->code : '';
                    watchdog("custom'', 'Twitter Account ' . $account_twitter['compte'] . ' failed to return results :' . $error);
                }
            }
        }
    }

Is the API slow ?
Anyone knows if the problem is with twitter ?


#2

The API isn’t particularly “slow” - you’re just doing all your requests one after the other. self::get() on the API SDK will fire off a request and block your script execution until the request comes back - which could take as much as a second or two based on your network latency.

Consider modifying the toolkit to do the requests in parallel using curl_multi.