jueves, 27 de enero de 2011

Obtener los tweets un usuario

En está ocasión de una manera sencilla vamos a obtener los ultimos mensajes de un usuario que llamaremos usuariox en Twitter:

Como recordaremos en el capitulo anterior de como usar CURL para acceder a los datos de una URL EXTERNA, usaremos nuestra funcion file_get_contents_curl para acceder a los ultimos tweets de usuariox de la siguiente manera:
  1. //nuestra función del artículo anterior:  
  2. function file_get_contents_curl($URL)  
  3.     {  
  4.         $c = curl_init();  
  5.         curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);  
  6.         curl_setopt($c, CURLOPT_URL, $URL);  
  7.         $contents = curl_exec($c);  
  8.         curl_close($c);  
  9.   
  10.         if ($contentsreturn $contents;  
  11.             else return FALSE;  
  12.     }  
  13. //Aquí la usamos:  
  14. $jsont = file_get_contents_curl("http://twitter.com/statuses/user_timeline/usuariox.json");  
  15. $jsont = json_decode($jsont, true);  
  16. //echo $json[0]['text'];  
  17.   
  18. // or display last 3  
  19. for($i=0; $i < 3 && isset($jsont[$i]); $i++) {  
  20. echo $jsont[$i]['text']."  
  21. ";  
  22. }  
Pero queremos mostrar como links los http, entonces haremos esto:
  1. //con este metodo cambiaremos todos los link como http o mailto a enlaces.  
  2. function make_links_blank($text)  
  3. {  
  4.   return  preg_replace(  
  5.      array(  
  6.        '/(?(?=<a[^>]*>.+<\/a>)  
  7.              (?:<a[^>]*>.+<\/a>)  
  8.              |  
  9.              ([^="\']?)((?:https?|ftp|bf2|):\/\/[^<> \n\r]+)  
  10.          )/iex',  
  11.        '/<a([^>]*)target="?[^"\']+"?/i',  
  12.        '/<a([^>]+)>/i',  
  13.        '/(^|\s)(www.[^<> \n\r]+)/iex',  
  14.        '/(([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-]+)  
  15.        (\\.[A-Za-z0-9-]+)*)/iex'  
  16.        ),  
  17.      array(  
  18.        "stripslashes((strlen('\\2')>0?'\\1<a href="\"\\2\"">\\2</a>\\3':'\\0'))",  
  19.        '<a\\1''<a\\1="" target="_blank">',  
  20.        "stripslashes((strlen('\\2')>0?'\\1<a href="\"http://\\2\"">\\2</a>\\3':'\\0'))",  
  21.        "stripslashes((strlen('\\2')>0?'<a href="\"mailto:\\0\"">\\0</a>':'\\0'))"  
  22.        ),  
  23.        $text  
  24.    );  
  25. }  
  26. function file_get_contents_curl($URL)  
  27.     {  
  28.         $c = curl_init();  
  29.         curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);  
  30.         curl_setopt($c, CURLOPT_URL, $URL);  
  31.         $contents = curl_exec($c);  
  32.         curl_close($c);  
  33.   
  34.         if ($contentsreturn $contents;  
  35.             else return FALSE;  
  36.     }  
  37. //Aquí la usamos:  
  38. $jsont = file_get_contents_curl("http://twitter.com/statuses/user_timeline/usuariox.json");  
  39. $jsont = json_decode($jsont, true);  
  40. //echo $json[0]['text'];  
  41.   
  42. // or display last 3  
  43. for($i=0; $i < 3 && isset($jsont[$i]); $i++) {  
  44. echo make_links_blank($jsont[$i]['text'])."  
  45. ";  
  46. }  
  47. </a\\1',></a([^></a([^></a[^></a[^>  

Related Posts:

  • Obtener los tweets un usuarioEn está ocasión de una manera sencilla vamos a obtener los ultimos mensajes de un usuario que llamaremos usuariox en Twitter: Como recordaremos en el capitulo anterior de como usar CURL para acceder a los datos de una URL EX… Read More

0 comentarios:

Publicar un comentario