Ene
Tips: Parseando con PHP el html de Youtube
-
<?
-
-
class Youtube{
-
function getVideosUser($user,$view){
-
//Carga la Web de Youtube
-
$ch = curl_init();
-
$timeout = 5; // si llega hasta 0 tiempo agotado
-
switch($view){
-
case "mis_videos":
-
$url='http://www.youtube.com/profile?user='.$user.'&view=videos';
-
break;
-
default:
-
$url='http://www.youtube.com/profile?user='.$user.'&view=favorites';
-
break;
-
}
-
curl_setopt ($ch, CURLOPT_URL,$url);
-
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
-
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
-
-
// Obtiene el HTML de Youtube
-
$file_contents = curl_exec($ch);
-
curl_close($ch);
-
$pagina=$file_contents;
-
// Utilizo expresiones regulares, en las partes que se repite el código
-
if(preg_match_all('!<img\s+title="([^"]*)"\s+src="([^"]*)"\s+class="vimg120"\s+qlicon="([^"&]+)"\s+alt="([^"]*)">!Usi', $pagina, $info, PREG_SET_ORDER)) {
-
foreach($info as $video) {
-
'titulo' => $video[1],
-
'image' => $video[2],
-
'id' => $video[3]
-
);
-
}
-
}
-
-
return $videos;
-
}
-
-
}
-
-
$videos=new Youtube();
-
echo "<pre>";
-
//Favoritos
-
//Mis videos
-
//print_r($videos->getVideosUser("bedomax","videos"));
-
echo "</pre>";
-
-
?>
Gracias que PHP hereda características de Perl, podemos utilizar expresiones regulares para manejar Strings. Aqui les dejo una clase que contiene un método que consulta los vídeos subidos y favoritos de un usuario de Youtube, ademas utilizo curl para obtener el html de Youtube y poder manipularlo con preg_match_all y obtengo en un array el resultado.
Array ( [0] => Array ( [titulo] =>Titulo del Video [image] => http://i4.ytimg.com/vi/k3kRuJhIVIo/default.jpg [id] => k3kRuJhIVIo ) )por: maximiliano con 0 comentarios en Código, PHP, Tips










