<div style='background-color: none transparent;'></div>

martes 10 de febrero de 2009

Twitter API

El Api de twitter
Bueno, ya que los apis estan de moda, yo estoy haciendo uno sencishito para mandar notas importantes a mi "tuiter", pero encontré algo mas complejo en la pagina de //snippets.webeame.net aquí el código, visiten su página de snippets, es muy interesante.


  1. <?php
  2. /*
  3. Ejemplo de instancia
  4. */
  5. require_once('class/twitter.class.php');
  6. /* Metodo Constructor para Twitter */
  7. $twitter = new Twitter('miusuario','mipass');
  8. // Ejemplo :)
  9. echo "<h1>Todos los mensajes de Fayerwayer</h1>";
  10. echo "<pre>";
  11. print_r($twitter->lineaTiempo('fayerwayer'));
  12. echo "</pre>";
  13. // Quieres realmente postear este mensaje? Descomentalo
  14. //$twitter->postearMensaje('Mi mensaje de pruebas desde API PHP para Twitter por framirez');
  15. // Todos mis seguidores
  16. echo "<h1>Todos mis seguidores</h1>";
  17. echo "<pre>";
  18. print_r($twitter->seguidores());
  19. echo "</pre>";
  20. // Ahora quiero ver todos mis mensajes
  21. echo "<h1>Todos mis mensajes</h1>";
  22. echo "<pre>";
  23. foreach($twitter->lineaTiempo() as $mensajes):
  24. echo "<p>Mensaje: " . $mensajes['mensaje'] . " posteado a las " . $mensajes['hora'] . " desde " . $mensajes['desde'] ."</p>";
  25. endforeach;
  26. echo "</pre>";
  27. <?php
  28. /*
  29. @author: Fabian Ramirez Sepulveda
  30. @email : framirez(ARROB)gurunet.cl
  31. @web : http://www.gurunet.cl/framirez
  32. @desc: Proyecto para tener una API desde PHP mas accesible y facil para usuarios no avanzados.
  33. @metodos:
  34. lineaTiempo( $nickNameAmigo=opcional) - "Nos retorna todos los ultimos 10 mensajes que nuestro amigo o nosotros hemos posteado"
  35. tomarMensaje($idmensaje) - "Nos retorna el detalle completo del mensaje"
  36. postearMensaje($mensaje) - "Posteamos en tiempo real el mensaje desde PHP"
  37. seguidores() - "Nos retorna todos nuestros amigos que nos siguen"
  38. */
  39. // Libreria necesaria para procesar Xpath
  40. require_once('XPath.class.php');
  41. class Twitter {
  42. var $usuario='';
  43. var $password='';
  44. // Headers de nuestro cliente
  45. var $agente = 'Twitter PHP Class by framirez';
  46. var $headers = array('X-Twitter-Client: Twitter PHP by framirez',
  47. 'X-Twitter-Client-Version: 1.0',
  48. 'X-Twitter-Client-URL: http://www.gurunet.cl/framirez');
  49. // Curl
  50. var $ch;
  51. // Respuesta
  52. var $respuesta;
  53. var $xml;
  54. function Twitter($usuario=null, $password=null) {
  55. $this->usuario = $usuario;
  56. $this->password = $password;
  57. // Metodo constructor automaticamente llama a Xpath
  58. $this->xml = new XPath();
  59. }
  60. /*
  61. @desc: Retornamos el maximo de 10 mensajes de amigos o mios
  62. */
  63. function lineaTiempo($nick = null) {
  64. if(empty($nick)):
  65. $this->ch = curl_init("http://twitter.com/statuses/user_timeline.xml");
  66. $this->xml->importFromString($this->setearOpcionesCurl());
  67. else:
  68. $this->ch = curl_init("http://twitter.com/statuses/user_timeline/" . $nick . ".xml");
  69. $this->xml->importFromString($this->setearOpcionesCurl());
  70. endif;
  71. // Xpath Match
  72. $id = $this->xml->match('//id');
  73. $nickname = $this->xml->match('//screen_name');
  74. $texto = $this->xml->match('//text');
  75. $hora = $this->xml->match('//created_at');
  76. $desde = $this->xml->match('//location');
  77. $deDonde = $this->xml->match('//source');
  78. // Variables Temporales
  79. $i=0;
  80. $arregloMensajes = array();
  81. // Recorro el arreglo
  82. for($i=0;$i<count($texto);$i++):
  83. $arregloMensajes[$i]['id'] = $this->xml->getData($id[$i]);
  84. $arregloMensajes[$i]['usuario'] = $this->xml->getData($nickname[$i]);
  85. $arregloMensajes[$i]['mensaje'] = $this->xml->getData($texto[$i]);
  86. $arregloMensajes[$i]['hora'] = $this->xml->getData($hora[$i]);
  87. $arregloMensajes[$i]['desde'] = $this->xml->getData($desde[$i]);
  88. $arregloMensajes[$i]['donde_proviene'] = $this->xml->getData($deDonde[$i]);
  89. $i++;
  90. endfor;
  91. $this->xml->reset();
  92. return $arregloMensajes;
  93. }
  94. function tomarMensaje($id) {
  95. $this->ch = curl_init("http://twitter.com/statuses/user_timeline.xml");
  96. $this->xml->importFromString($this->setearOpcionesCurl());
  97. // Xpath Match
  98. $id = $this->xml->match('//id');
  99. $nickname = $this->xml->match('//screen_name');
  100. $texto = $this->xml->match('//text');
  101. $hora = $this->xml->match('//created_at');
  102. $desde = $this->xml->match('//location');
  103. $deDonde = $this->xml->match('//source');
  104. // Variables Temporales
  105. $arregloMensajes = array();
  106. // Recorro el arreglo
  107. $arregloMensajes['id'] = $this->xml->getData($id[0]);
  108. $arregloMensajes['usuario'] = $this->xml->getData($nickname[0]);
  109. $arregloMensajes['mensaje'] = $this->xml->getData($texto[0]);
  110. $arregloMensajes['hora'] = $this->xml->getData($hora[0]);
  111. $arregloMensajes['desde'] = $this->xml->getData($desde[0]);
  112. $arregloMensajes['donde_proviene'] = $this->xml->getData($deDonde[0]);
  113. $this->xml->reset();
  114. return $arregloMensajes;
  115. }
  116. function postearMensaje($mensaje) {
  117. if(empty($mensaje)):
  118. die("Debes pasar como parametro el mensaje");
  119. else:
  120. $this->ch = curl_init("http://twitter.com/statuses/update.xml");
  121. $this->xml->importFromString($this->setearOpcionesCurl('status=' . urlencode($mensaje)));
  122. $this->xml->reset();
  123. endif;
  124. return true;
  125. }
  126. function seguidores() {
  127. $this->ch = curl_init("http://twitter.com/statuses/followers.xml");
  128. $this->xml->importFromString($this->setearOpcionesCurl());
  129. // Xpath Match
  130. $id = $this->xml->match('//id');
  131. $nickname = $this->xml->match('//screen_name');
  132. $nombre = $this->xml->match('//name');
  133. $desde = $this->xml->match('//location');
  134. $web = $this->xml->match('//url');
  135. $foto = $this->xml->match('//profile_image_url');
  136. $descripcion = $this->xml->match('//description');
  137. // Variables Temporales
  138. $i=0;
  139. $arregloMensajes = array();
  140. // Recorro el arreglo
  141. for($i=0;$i<count($nickname);$i++):
  142. $arregloMensajes[$i]['id'] = $this->xml->getData($id[$i]);
  143. $arregloMensajes[$i]['nombre'] = $this->xml->getData($nombre[$i]);
  144. $arregloMensajes[$i]['desde'] = $this->xml->getData($desde[$i]);
  145. $arregloMensajes[$i]['usuario'] = $this->xml->getData($nickname[$i]);
  146. $arregloMensajes[$i]['web'] = $this->xml->getData($web[$i]);
  147. $arregloMensajes[$i]['foto'] = $this->xml->getData($foto[$i]);
  148. $arregloMensajes[$i]['descripcion'] = $this->xml->getData($descripcion[$i]);
  149. $i++;
  150. endfor;
  151. $this->xml->reset();
  152. return $arregloMensajes;
  153. }
  154. function setearOpcionesCurl($post=false) {
  155. // Autentificamos
  156. curl_setopt($this->ch, CURLOPT_USERPWD, $this->usuario.':'.$this->password);
  157. if(!empty($post)){
  158. curl_setopt($this->ch, CURLOPT_POST, true);
  159. curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
  160. }
  161. curl_setopt($this->ch, CURLOPT_VERBOSE, 1);
  162. curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
  163. curl_setopt($this->ch, CURLOPT_USERAGENT, $this->agente);
  164. // Setamos la respuesta
  165. $respuesta = curl_exec($this->ch);
  166. curl_close($this->ch);
  167. unset($this->ch);
  168. return $respuesta;
  169. }
  170. }
  171. ?>

0 comentarios:

Publicar un comentario en la entrada

Venga, exprésate