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:
- //nuestra función del artículo anterior:
- function file_get_contents_curl($URL)
- {
- $c = curl_init();
- curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($c, CURLOPT_URL, $URL);
- $contents = curl_exec($c);
- curl_close($c);
- if ($contents) return $contents;
- else return FALSE;
- }
- //Aquí la usamos:
- $jsont = file_get_contents_curl("http://twitter.com/statuses/user_timeline/usuariox.json");
- $jsont = json_decode($jsont, true);
- //echo $json[0]['text'];
- // or display last 3
- for($i=0; $i < 3 && isset($jsont[$i]); $i++) {
- echo $jsont[$i]['text']."
- ";
- }
- //con este metodo cambiaremos todos los link como http o mailto a enlaces.
- function make_links_blank($text)
- {
- return preg_replace(
- array(
- '/(?(?=<a[^>]*>.+<\/a>)
- (?:<a[^>]*>.+<\/a>)
- |
- ([^="\']?)((?:https?|ftp|bf2|):\/\/[^<> \n\r]+)
- )/iex',
- '/<a([^>]*)target="?[^"\']+"?/i',
- '/<a([^>]+)>/i',
- '/(^|\s)(www.[^<> \n\r]+)/iex',
- '/(([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-]+)
- (\\.[A-Za-z0-9-]+)*)/iex'
- ),
- array(
- "stripslashes((strlen('\\2')>0?'\\1<a href="\"\\2\"">\\2</a>\\3':'\\0'))",
- '<a\\1', '<a\\1="" target="_blank">',
- "stripslashes((strlen('\\2')>0?'\\1<a href="\"http://\\2\"">\\2</a>\\3':'\\0'))",
- "stripslashes((strlen('\\2')>0?'<a href="\"mailto:\\0\"">\\0</a>':'\\0'))"
- ),
- $text
- );
- }
- function file_get_contents_curl($URL)
- {
- $c = curl_init();
- curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($c, CURLOPT_URL, $URL);
- $contents = curl_exec($c);
- curl_close($c);
- if ($contents) return $contents;
- else return FALSE;
- }
- //Aquí la usamos:
- $jsont = file_get_contents_curl("http://twitter.com/statuses/user_timeline/usuariox.json");
- $jsont = json_decode($jsont, true);
- //echo $json[0]['text'];
- // or display last 3
- for($i=0; $i < 3 && isset($jsont[$i]); $i++) {
- echo make_links_blank($jsont[$i]['text'])."
- ";
- }
- </a\\1',></a([^></a([^></a[^></a[^>
0 comentarios:
Publicar un comentario