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:
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']."
";
}
Pero queremos mostrar como links los http, entonces haremos esto:
//con este metodo cambiaremos todos los link como http o mailto a enlaces.
function make_links_blank($text)
{
return preg_replace(
array(
'/(?(?=]*>.+<\/a>)
(?:]*>.+<\/a>)
|
([^="\']?)((?:https?|ftp|bf2|):\/\/[^<> \n\r]+)
)/iex',
'/]*)target="?[^"\']+"?/i',
'/]+)>/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\\2\\3':'\\0'))",
'',
"stripslashes((strlen('\\2')>0?'\\1\\2\\3':'\\0'))",
"stripslashes((strlen('\\2')>0?'\\0':'\\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'])."
";
}





