DMT - Tweet mediante cURL y PHP
Estaba intentado crear un script para un post de mi foro favorito, cuando vi que Twitter ya había removido "the basic authentication" que era mediante cUrl y un par de headers...
Así que me acorde de una vez que hice un script para cambiar el estado de Tuenti mediante php. Este utilizaba la versión móvil, (donde apenas o directamente no había javascript) para identificarse y cambiar el estado. Dado que Twitter es prácticamente lo mismo es fácil.
La cuestión es que twitter verifica unicamente un "key" para enviar los formularios y es bastante fácil.
Sin mas, aquí esta la clase... por ahora solo permite postear en tweets pero seguramente pronto la actualice para que se parezca como a "la vieja API" de twitter.
Su uso es bastante fácil..
Lo he hecho en ingles porque me gusta mas pero la traduccion y lo que hace son obvios...
He incluido un parametro adicional que es el de poder personalizar el user-agent (navegador). Para hacerlo solo hace falta llamar la función set_user_agent.
Pues sin mas esto es todo.
Saludos
Así que me acorde de una vez que hice un script para cambiar el estado de Tuenti mediante php. Este utilizaba la versión móvil, (donde apenas o directamente no había javascript) para identificarse y cambiar el estado. Dado que Twitter es prácticamente lo mismo es fácil.
La cuestión es que twitter verifica unicamente un "key" para enviar los formularios y es bastante fácil.
Sin mas, aquí esta la clase... por ahora solo permite postear en tweets pero seguramente pronto la actualice para que se parezca como a "la vieja API" de twitter.
<?php class drvyMobileTwitter { /* Name: Drvy Mobile Twitter Desc: Post to twitter by using curl in the mobile version. Version: 1.0 Author: Dragomir Valentinov Yourdanov (a.k.a Drvymonkey) License: Drvy Mobile Twitter by Dragomir Valentinov Yourdanov is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License. http://creativecommons.org/licenses/by-nc-nd/3.0/ */ private function from_to($from, $to, $target){ // Function by WHK (drawcoders.com) // modified by DrvyMonkey (drvymonkey.com) due to "eregi" being deprecated // Returns string between $from and $to. if(stristr($target,$from)){ $return= explode($from, $target); $return= $return[1]; $return= explode($to, $return); $return= $return[0]; return $return; } else { return FALSE;} } public function set_user_agent($param){ // Sets personal user-agent global $usragnt; $usragnt= $param; } private function request_die($why){ // In case of fatal errors, this function will be called. // Ends any process related to this script. die('<h1>Die requested</h1>'.$why); } public function register_tmp_cookie($cookiename){ // Attempts to create a temporaly cookie // to use with cUrl. if($cookie= tempnam ("/tmp", $cookiename)){return $cookie;} else {$this->request_die('<b>Cant create cookie file.</b>');} } public function dmt_connect($username,$password,$tmpcookie){ // Attemps to login into twitter mobile if(empty($usragnt)){$usragnt= 'Opera/9.80 (J2ME/MIDP; Opera Mini/6.24093/25.657;)';} $twittermurl= 'https://mobile.twitter.com/session/new'; global $ch; $ch= curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, $usragnt); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_URL, $twittermurl); curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpcookie); curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpcookie); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output= curl_exec($ch); $from= 'type="hidden" value="'; $to= '"'; $auth= $this->from_to($from,$to,$output); $twittermurl= 'https://mobile.twitter.com/session'; curl_setopt($ch, CURLOPT_URL, $twittermurl); curl_setopt ($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ch, CURLOPT_POSTFIELDS, "authenticity_token=".$auth."&username=".$username."&"."password=".$password); $output= curl_exec($ch); if(stristr($output,"What's happening?")){return TRUE;} else {$this->request_die('<b>Cant log in.</b>');} } public function dmt_post($post,$tmpcookie){ // Posts to twitter $post= substr(urlencode($post),0,140); global $ch; $twittermurl= 'https://mobile.twitter.com'; curl_setopt($ch, CURLOPT_URL, $twittermurl); curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpcookie); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $output= curl_exec($ch); $from= 'type="hidden" value="'; $to= '"'; $auth= $this->from_to($from,$to,$output); curl_setopt ($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ch, CURLOPT_POSTFIELDS, "authenticity_token=".$auth. "&tweet%5Btext%5D=".$post); $output= curl_exec($ch); if(stristr($output,$post)){return TRUE;} else {return FALSE;} } public function dmt_close_connection($cookie){ // Kill's cURL and attempts to remove the cookie. global $ch; curl_close($ch); @unlink($cookie); } } ?>
Su uso es bastante fácil..
// Open class $mtwitter= new drvyMobileTwitter(); // Create a temporal cookie) $cookie= $mtwitter->register_tmp_cookie('chcookie'); // Connect to twitter (user,password,cookie) $mtwitter->dmt_connect('user','password',$cookie); // Post to twitter $mtwitter->dmt_post('Hi, Im Crazy!!!',$cookie); // Close cUrl and attempt to delete cookie $mtwitter->dmt_close_connection($cookie);
Lo he hecho en ingles porque me gusta mas pero la traduccion y lo que hace son obvios...
He incluido un parametro adicional que es el de poder personalizar el user-agent (navegador). Para hacerlo solo hace falta llamar la función set_user_agent.
// Set custom user-agent $mtwitter->set_user_agent('Super-Ultra-Cool Navigator (only for monkeys)');
Pues sin mas esto es todo.
Saludos
Comentarios
Publicar un comentario