Twitter API call in the while loop is too slow for some reason , I'm using this PHP script


So I have this PHP script below that requests data from the Twitter API and then displays it on the page according to the information that I have in my local database because I stored twitter usernames of some twitter users so I can pass them to the API and display the profile images of those twitter users. The script works fine to display my request from the API but it is AWFULLY slow , so I was wondering what could cause the script to be so slow … Below is my script … the first part seems to execute fine…

    /* Create a TwitterOauth object with consumer/user tokens. */
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
    $url = $connection->get('statuses/user_timeline', array('screen_name' => $row['Twitter'],count=>'1'));
             $results  = json_encode($url);
             $data = json_decode($results, true);
             $image = '';  
             $image = $data[0]['user']['profile_image_url']; 
             $image_bigger = str_replace('_normal', '_bigger',$image); 
     echo "<h1 align='middle' id = $id> $name Vs Opposition </h1>";
     echo "<img src='".$image_bigger."' width= '100' height ='100' class= 'image' align='middle' />";
//This part of the script below seems to be the culprit of the page taking so long to display info because it loops through all the twitter IDs returned by the query in order to display the images of the authors that I request through the API inside the loop . $data2 variable is very long array of info regarding the twitter user and from that array , I pick the key of the array containing the value that stores the picture of every twitter user inside a loop .. How could I dynamically optimize this so the page can load faster?  
    $select2  = "SELECT * FROM AUTHORS WHERE ID <> $id";  
    $result2 = mysql_query($select2);
    $result_count = mysql_num_rows($result2);
    $image_array = array();
    $unique_id = array();
    $counter = 0;
    if($result_count > 0) { 
    while ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) { 
    array_push($unique_id, $row2['ID']);
    $url2 = $connection->get('statuses/user_timeline', array('screen_name' => $row2['Twitter'],count=>'1'));
             $results2  = json_encode($url2);
             $data2 = json_decode($results2, true);   // data through is a very long array
              $image2 = $data2[$counter]['user']['profile_image_url'];  
              $image3 = str_replace('_normal', '_bigger',$image2);
     // the while loop above seems to be the culprit in the page taking too long to load ... is there anyway I could optimize this ?
    <div id ="opposition">
     $unique_image = array_unique($image_array);
     $id_array = array_unique($unique_id);
      $i = 0;
      foreach($unique_image as $content) {
     echo "<a id ='".$id_array[$i]."' href ='#'><img src='".$content."'  width= '100' height ='100'  class='image' /></a>";


Hi, did you get a solution to the slowness?


For a start, remember that every time you make an API call, you’re effectively firing a HTTP request. This is not optimal in your case.

Consider the following options:

  • Rewrite the API hooks of your PHP script to use curl_multi rather than straight curl. This will allow you to run all your timeline requests in parallel, which will speed it up significantly. This will require some PHP know-how.
  • Cache the timeline info and process it outside of your page cycle. Provided that you stick by the twitter API ToS (i.e. only store tweet IDs and/or limited caching in time), this will prove to be the fastest solution.

In both cases, more code info is needed. If you’d like more advice, feel free to PM me.