Existem uma infinidades de serviços na web que se oferecem para exibir seu número de followers no Twitter, geralmente em forma de botões, porém, além de exibir o próprio logotipo, essas ferramentas não dão ao usuário muita liberdade de personalização.
A solução então é usarmos o PHP e nossa criatividade para criar nossos próprios botões de seguidores do Twitter.
O código é bem simples, consiste basicamente em uma função que faz uma consulta XML a API do Twitter e retorna o número de followers, o exemplo do resultado pode ser visto em nossa barra lateral: → → →
<?php
/**
* Fetch the number of followers from twitter api
*
* @author Peter Ivanov
* @copyright http://www.ooyes.net
* @version 0.2
* @link http://www.ooyes.net
* @param string $username
* @return string
*/
function twitter_followers_counter($username) {
$cache_file = CACHEDIR . 'twitter_followers_counter_' . md5 ( $username );
if (is_file ( $cache_file ) == false) {
$cache_file_time = strtotime ( '1984-01-11 07:15' );
} else {
$cache_file_time = filemtime ( $cache_file );
}
$now = strtotime ( date ( 'Y-m-d H:i:s' ) );
$api_call = $cache_file_time;
$difference = $now - $api_call;
$api_time_seconds = 1800;
if ($difference >= $api_time_seconds) {
$api_page = 'http://twitter.com/users/show/' . $username;
$xml = file_get_contents ( $api_page );
$profile = new SimpleXMLElement ( $xml );
$count = $profile->followers_count;
if (is_file ( $cache_file ) == true) {
unlink ( $cache_file );
}
touch ( $cache_file );
file_put_contents ( $cache_file, strval ( $count ) );
return strval ( $count );
} else {
$count = file_get_contents ( $cache_file );
return strval ( $count );
}
}
?> |
<?php
/**
* Fetch the number of followers from twitter api
*
* @author Peter Ivanov
* @copyright http://www.ooyes.net
* @version 0.2
* @link http://www.ooyes.net
* @param string $username
* @return string
*/
function twitter_followers_counter($username) {
$cache_file = CACHEDIR . 'twitter_followers_counter_' . md5 ( $username );
if (is_file ( $cache_file ) == false) {
$cache_file_time = strtotime ( '1984-01-11 07:15' );
} else {
$cache_file_time = filemtime ( $cache_file );
}
$now = strtotime ( date ( 'Y-m-d H:i:s' ) );
$api_call = $cache_file_time;
$difference = $now - $api_call;
$api_time_seconds = 1800;
if ($difference >= $api_time_seconds) {
$api_page = 'http://twitter.com/users/show/' . $username;
$xml = file_get_contents ( $api_page );
$profile = new SimpleXMLElement ( $xml );
$count = $profile->followers_count;
if (is_file ( $cache_file ) == true) {
unlink ( $cache_file );
}
touch ( $cache_file );
file_put_contents ( $cache_file, strval ( $count ) );
return strval ( $count );
} else {
$count = file_get_contents ( $cache_file );
return strval ( $count );
}
}
?>
Nota: O twitter limita a quantidade de pings a API em 150 por hora, por isso a função cria um cache que é salvo em um arquivo de texto.
Para imprimir o resultado:
<?php echo twitter_followers_counter('user_do_Twitter'); ?> |
<?php echo twitter_followers_counter('user_do_Twitter'); ?>
[Créditos]
alguém opinou!
Deixe sua opinião!One Ping
Pingback:Pegando o número de seguidores do Twitter via PHP | Rotina Digital | ZiiPe