Twitter API call is EXTREMELY SLOW ... What could be causing this ? Below is my script .. Many thanks!


#1

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 = '';  
             if(is_array($data)){  
             $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' />";
    ?>
 </code>
//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?  
   
 <?php
    $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);
              array_push($image_array,$image3); 
    
           }
     }
     // 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">
    <?php
     $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>";
     $i++;
    }
    ?>
    </div>