Enviar mensajes (tweets) a Twitter con PHP

Encontre una clase (que ya tiene buen tiempo en la web) de Antonio Lupetti que ultimamente he estado usando para el microblogging en Twitter. Bastará con tener instalado las librerias CURL (permite conexión y comunicación con varios tipos de servidores diferentes con varios tipos de protocolos diferentes) en PHP. He aquí la clase y la forma de usarlo en nuestro sitio web.

<?php
/*
File: twitter.php

Description:
The following class is for using twitter.com

This class was based off the work of Antonio Lupetti. Original Work can be found at:
http://woork.blogspot.com/2007/10/twitter-send-message-from-php-page.html

Contributing Author( s):
Antonio Lupetti < antonio.lupetti@gmail.com >
Scott Sloan < scott@aimclear.com >

Date: January 4th, 2008
License: Creative Commons

*/
class twitter {

private $user;
private $pass;
private $ch;
private $twitterHost = "http://twitter.com/";

public function __construct($username, $passwd) {
$this->user = $username;
$this->pass = $passwd;

/* Create and setup the curl Session */
$this->ch = curl_init();

curl_setopt($this->ch, CURLOPT_VERBOSE, 1);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_USERPWD, "$this->user:$this->pass");
curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->ch, CURLOPT_POST, 1);
}

public function __destruct() {
/*clean Up */
curl_close($this->ch);
}

public function setStatus($stat) {

if(strlen($stat) < 1)
return false;

/*Set the host information for the curl session */
$this->twitterHost .= "statuses/update.xml?status=". urlencode(stripslashes(urldecode($stat)));

curl_setopt($this->ch, CURLOPT_URL, $this->twitterHost);

/* Execute and get the http code to see if it was succesfull or not*/
$result = curl_exec($this->ch);
$resultArray = curl_getinfo($this->ch);

if ($resultArray['http_code'] == 200) ;
return true;

return false;
}
}
?>

El proceso y formulario para los mensajes (tweets).

<?php
include_once('twitter.php');

$curTwitter = new twitter("usuario", "password");

if (isset($_POST['submit'])) {

$twitter_status = $_POST['twitter_stat'];

if (strlen($twitter_status) > 0) {

if( $curTwitter->setStatus($twitter_status) == true)
echo "<p>Updated Succesfully</p>";
else
echo "<p>Twitter is unavailable at this time</p>";
} else
echo "<p>Error: I won't send blank messages!</p>";
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Set your status on Twitter</title>
</head>
<body>
<h2>Set your status on Twitter</h2>

<p><strong>What are you doing?</strong></p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="twitter_stat" type="text" id="twitter_stat" size="40" maxlength="140" />
<input type="submit" name="submit" id="submit" value="Send"/>
</form>

</body>
</html>

Comentarios Comentarios formato RSS

  1. avatar Juan Carlos 2009-12-01 12:37:06 5 hola me gusto mucho tu ayuda pero a la hora que pongo a otro cliente no actualiza la lista...
  2. avatar sysmaya 2009-08-10 22:32:07 4 mirando por ahi vi este blog:

    http://blog.oridoki.com/2009/06/15/php-enviar-post-a-twitter/

    tiene una funcion muy mu corta como 10 lineas y que aparentemente hace lo mismo.

    es cuestion de probar
  3. avatar jesusvld 2009-08-05 09:11:13 3 @all:

    Pueden probar revisar este articulo: http://dy3g0.wordpress.com/2008/12/09/activar-curl-en-windows/ , pues no se trata de descomentar la librería. Espero les sirva.

    Saludos
  4. avatar Daniel Quimbayo 2009-08-04 15:11:35 2 Hola, prove la clase y el aplicativo y no funcionan, revise el phpinfo(); y esta habilitado la libreria Curl, no entiendo que pasa, por fa es urgente
  5. avatar Hugo 2009-07-31 09:56:53 1 Disculpa pero he probado tu clase y ni me funciona aunque diga que el mensaje se envio. sugerencias. ahh y si tengo habilitados los modulos correspondientes

Dejar un comentario