segunda-feira, 12 de dezembro de 2011

(PHP) twitterLogin e twitterTweet

Vendo fóruns e funções velhas do orkut consegui refazer uma função para logar no twitter e até mesmo dar tweets. Bem fácil.

Segue código:
function twitterLogin($email, $password)
{

$ch = curl_init();
curl_setopt_array($ch, Array(
CURLOPT_URL => 'https://mobile.twitter.com/session/new',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true)
);

$response = curl_exec($ch);
preg_match("/_mobile_sess=[^\s]+/", $response, $_mobile_sess);
preg_match("/name=\"authenticity_token\" type=\"hidden\" value=\"([^\"]+)/", $response, $authenticity_token);
curl_setopt_array($ch, Array(
CURLOPT_URL => 'https://mobile.twitter.com/session',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_COOKIE => $_mobile_sess[0],
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => 'authenticity_token=' . $authenticity_token . '&username=' . trim(rawurlencode($email)) . '&password=' . trim(rawurlencode($password)),
CURLOPT_HEADER => true)
);

$response = curl_exec($ch);

preg_match("/k=[^\s]+/", $response, $k);
preg_match("/_mobile_sess=[^\s]+/", $response, $mobile);
preg_match("/oauth_token=[^\s]+/", $response, $oauth);
curl_close($ch);

return $k[0] . $mobile[0] . $oauth[0];
}

function twitterTweet($login, $senha, $tweet)
{
$cookies = twitterLogin($login,$senha);

$ch = curl_init();

curl_setopt_array($ch, Array(
CURLOPT_URL => 'http://mobile.twitter.com/' . $login,
CURLOPT_COOKIE => $cookies,
CURLOPT_RETURNTRANSFER => true)
);

$response = curl_exec($ch);
preg_match("/name=\"authenticity_token\" type=\"hidden\" value=\"([^\"]+)/", $response, $authenticity);
$ch = curl_init();

curl_setopt_array($ch, Array(
CURLOPT_URL => 'http://mobile.twitter.com/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_REFERER => 'mobile.twitter.com/',
CURLOPT_COOKIE => $cookies,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_POSTFIELDS => '&tweet[text]='.$tweet.'&authenticity_token=' . $authenticity[1] . '&last_url=%2F' . rawurlencode($login))
);

preg_match('/href="([^\"]{0,500})/i', curl_exec($ch), $href);
return ($href[1]==="http://mobile.twitter.com/");
}

2 comentários:

  1. Utiliza algum hosting grátis. Por exemplo 6te, miarroba, 000webhost. Tem vários, só procurar um que tenha suporte php.

    Abraço

    ResponderExcluir