Ainda não descobri a real utilidade do Twitter, mas seu uso como ferramenta de marketing viral vem crescendo enormemente.
Pensando nisso, eu resolvi desenvolver uma forma de ‘tuítar’ diretamente de um site meu sempre que esse fosse atualizado (como o wordpress faz), incluindo a URL encurtada com o link para a atualização, e explico, no código abaixo, como fazer isso.
Considerações:
- Pro código funcionar, é necessário baixar a classe Twitter (o endereço está no código) e possuir uma conta no Twitter.
- Se desejar inserir uma URL encurtada no seu ‘Twit’ é necessário também uma conta no serviço Bit.ly (existem muitos outros mas este é usado nessa demonstração)
-
//Iniciando as variáveis
-
//Do bit.ly
-
//Para utilizar a API do bit.ly, basta possuir uma conta registrada(gratuitamente)
-
//Seu login
-
$blogin = ”;
-
//Sua API key
-
$bkey = ”;
-
-
//Do Twitter
-
$tlogin = ”;
-
$tpass = ”;
-
-
//Texto a ser inserido no twitter
-
//The number of the beast
-
//Letra e música de IRON MAIDEN
-
-
$iron = "’Woe to you, Oh Earth and Sea,
-
for the Devil sends the beast with wrath,
-
because he knows the time is short…
-
Let him who hath understanding reckon
-
the number of the beast for it is a human number,
-
it’s number is Six hundred and sixty six’
-
-
I left alone, my mind was blank.
-
I needed time to think to get the memories from my mind.
-
-
What did I see, can I believe,
-
That what I saw that night was real
-
and not just fantasy.
-
-
Just what I saw, in my old dreams,
-
Were they reflections of my warped mind staring back at me.
-
-
‘Cause in my dreams, it’s always there,
-
The evil face that twists my mind and brings me to despair.
-
Yeah!!!
-
-
The night was black, was no use holding back,
-
‘Cause I just had to see, was someone
-
watching me.
-
In the mist, dark figures move and twist,
-
Was all this for real, or just some kind of hell.
-
-
Six, six, six the number of the beast.
-
Hell and fire was spawned to be released.
-
-
Torches blazed and sacred chants were praised,
-
As they start to cry, hands held to the sky.
-
In the night, the fires burning bright,
-
The ritual has begun, Satan’s work is done.
-
-
Six, six, six the number of the beast.
-
Sacrifice is going on tonight.
-
-
This can’t go on, I must inform the law.
-
Can this still be real or just some crazy dream.
-
But I feel drawn towards the evil chanting hordes,
-
They seem to mesmerize…can’t avoid their eyes,
-
-
Six, six, six the number of the beast.
-
Six, six, six the one for you and me.
-
-
I’m coming back, I will return,
-
And I’ll possess your body and I’ll make you burn.
-
I have the fire, I have the force.
-
I have the power to make my evil take its course.";
-
-
//Codificando o texto para utf8 (necessário para acentos e etc)
-
-
//Mãos à obra
-
-
//Primeiro, encurtamos a URL utilizando a API do bit.ly
-
// Iniciamos o CURL
-
$ch = curl_init();
-
-
//O parâmetro CURLOPT_RETURNTRANSFER com valor 1 vai fazer com que curl_exec retorne a página
-
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
-
-
//Inserimos a URL a ser encurtada
-
$url = ‘http://melhordaputaria.com.br’;
-
-
// É necessário codificar a URL para funcionar
-
-
//Acessamos a API do bit.ly solicitando a URL encurtada e a resposta
-
//em formato texto
-
curl_setopt($ch, CURLOPT_URL, "http://api.bit.ly/v3/shorten?login=$blogin&apiKey=$bkey&longUrl=$long&format=txt");
-
-
//A variável $short recebe a URL encurtada
-
$short = curl_exec ($ch);
-
-
// Fechamos o CURL
-
curl_close ($ch);
-
-
//Hora de criar a mensagem a ser inserida no Twitter (com, no máximo, 140 caracteres)
-
//Contamos o tamanho do texto, mais o tamanho da URL encurtada mais 1 (Pra colocar um caracter ‘-’ entre um e outro)
-
-
//Garantimos que o texto a ser atualizado tenha até 140 caracteres (matemática simples)
-
else
-
$msg = $txt . ‘-’ . $short;
-
-
//Inserimos a classe Twitter
-
//disponível em http://www.phpclasses.org/package/4216-PHP-Update-the-status-of-a-Twitter-user.html
-
require "Twitter.class.php";
-
-
// Instanciamos um objeto Twitter, passando o login e senha do Twitter a ser atualizado
-
$tweet = new Twitter($tlogin, $tpass);
-
-
//Finalmente, atualizamos o Twitter
-
$success = $tweet->update($msg);
-
if ($success)
-
//Mostramos uma mensagem de sucesso
-
echo "Funciona!";
-
else
-
//Ou o erro ocorrido
-
-
Dúvidas e sugestões?? Deixe um comentário