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.
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: