Help with Twitter API

Friends, I am trying to get the data of Twitter users by ID, via API, but as I’m using WHILE is VERY slow. My script is correct? Can you help me find a solution?

<?php
    require_once('config.php');

    function user($id){
        $jsonurl = "https://api.twitter.com/1/users/show.json?id=".$id."&include_entities=true";

        //Retorna o conteudo do arquivo em formato de string
        $json = file_get_contents($jsonurl,0,null,null);

        //Decodificando a string e criando o json
        $json_output = json_decode($json);

        return $json_output->screen_name;
    }

    function img($id){
        $jsonurl = "https://api.twitter.com/1/users/show.json?id=".$id."&include_entities=true";

        //Retorna o conteudo do arquivo em formato de string
        $json = file_get_contents($jsonurl,0,null,null);

        //Decodificando a string e criando o json
        $json_output = json_decode($json);

        return $json_output->profile_image_url;
    }

    $sql = mysql_query("SELECT * FROM content");
    while ( $filter = mysql_fetch_object($sql) )
    {
      echo '<img src="'.img($filter->user).'" width="50" height="50" alt="" /><br />';
      echo user($filter->user). '<br /><br />';
    }

You can use users/lookup to get up to 100 user data at once. Also you don’t need to lookup each user’s image, because it’s already returned with user data in field profile_image_url. You can replace normal with bigger, mini or original in that url if you need different size of profile image.

I already tried that. The problem is not in the picture, but in programming. = /

Any suggestions, folks?

This should help you get started.

<?php
$users = array();
//get user ids from database
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
$query = "SELECT * FROM users LIMIT 100";

foreach($dbh->query($query) as $row) {
    $users[] = $row['twitter_id'];
}

$request  = "https://api.twitter.com/1/users/lookup.json?user_id=".implode(',', $users);

$content = json_decode(file_get_contents($request));

foreach($content as $user) {?>
    <div>
        <img src="<?php echo $user->profile_image_url; ?>" alt="" />
        <a href="http://twitter.com/<?php echo $user->screen_name; ?>"><?php echo $user->name; ?></a>
    </div>
<?php } ?>

Worked perfectly, thanks.

I did not know this PHP PDO. Can you tell me why I can not do this?

$query = mysql_query("SELECT * FROM conteudo");

foreach($query as $row) {
     $users[] = $row['usuario'];
} 

You can use ORDER BY with PHP PDO? For me it did not work. = /

Of course, you can use any valid SQL statement with PDO. It’s suggested to switch to PDO or MySQLi, because regular MySQL extension is outdated and will get removed in the future.
Problem in your snippet is that you need fetch the rows:


$query = mysql_query("SELECT * FROM conteudo");  

while($row = mysql_fetch_array($query)) {  
     $users[] = $row['usuario'];  
} 

For easier debugging, enable error_reporting and use mysql_errors() to catch query errors.

Thank you. How do I sort the results? Every time I refreshed the page the order changes, even with the ORDER BY.

ORDER BY should be working, if you mean sorting database results.
Show the code which you are using.