espero les sirva.
![Aplauso](http://static.forosdelweb.com/fdwtheme/images/smilies/aplausos.gif)
Código PHP:
<?php
include_once("../Conexion/Conx.php");
include_once("../Funciones/Funciones.php");
class _Buscador_Youtube {
/*
@ public static $q - Cadena a buscar
*/
public static $q;
/*
@ public static $dev_id - ID API Youtube
*/
public static $dev_id = "ugP3HnT7ruo";
/*
@ public static $TotalResultados - Número total de result.
*/
public static $TotalResultados = 20;
/*
@ public static $DireccionAPI - API Youtube
*/
public static $DireccionAPI = "http://www.youtube.com/api2_rest?";
public static $pag;
public static $id, $Flv, $Mp4, $En3gp, $Titulo, $Autor, $Tags, $Duracion,
$Vistas, $Descripcion, $Img;
private static function _ExtraerCodigoVideo($url) {
if(function_exists('file_get_contents')) $html = @file_get_contents($url);
else $html = @join('',file($url));
return str_replace(array("\r","\n"),'',$html);
}
private static function id($url) {
$id = explode('=',$url);
$id = explode('&',$id['1']);
return $id['0'];
}
private static function Seg_Min($seg) {
return floor($seg/60).':'.($seg%60);
}
public static function Video($url) {
$id = self::id($url);
//$html = self::_ExtraerCodigoVideo('http://www.youtube.com/watch?v='.$id);
//preg_match('#"t": "(.*?)",#',$html,$t);
//self::$Flv = 'http://www.youtube.com/get_video?video_id='.$id.'&t='.$t['1'];
//self::$Mp4 = 'http://www.youtube.com/get_video?fmt=18&video_id='.$id.'&t='.$t['1'];
$xml = self::_ExtraerCodigoVideo('http://gdata.youtube.com/feeds/api/videos/'.$id);
preg_match('#<media:title type=\'plain\'>(.*?)</media:title>#',$xml,$title);
preg_match('#<name>(.*?)</name>#',$xml,$name);
preg_match('#<media:keywords>(.*?)</media:keywords>#',$xml,$tag);
preg_match('#<yt:duration seconds=\'(.*?)\'/>#',$xml,$time);
preg_match('#<yt:statistics viewCount=\'(.*?)\'/>#',$xml,$stat);
preg_match('#<media:description type=\'plain\'>(.*?)</media:description>#',$xml,$descri);
preg_match_all('#content url=\'(.*?)\'#',$xml,$cel);
self::$id = $id;
self::$Img = 'http://i.ytimg.com/vi/'.$id.'/default.jpg';
self::$Tags = $tag['1'];
self::$Autor = $name['1'];
self::$Vistas = $stat['1'];
self::$En3gp = $cel['1']['2'];
self::$Titulo = $title['1'];
self::$Duracion = self::Seg_Min($time['1']);
self::$Descripcion = $descri['1'];
}
private static function _CodificarTexto($t){
return(iconv("UTF-8", "ISO-8859-1", $t));
}
private static function _Pag(){
self::$pag = (!empty($_GET['pag']) && eregi("^[0-9]+$",$_GET['pag'])) ? ($_GET['pag']) : (1) ;
return(self::$pag);
}
private static function _GET() {
foreach ($_GET as $nombre => $valor) {
if ($nombre != 'pag' && $nombre != 'v') {
$valor = urlencode($valor) ;
$url .= $nombre."=".$valor."&" ;
}
}
return $_SERVER['PHP_SELF']."?".$url ;
}
private static function _q() {
if(!empty($_GET['q'])){$q = $_GET['q'];} else {
$Palabras = Array('lima','cusco','ceviche','arequipa','turismo','politica');
$q = "peru+".$Palabras[rand(0,(count($Palabras)-1))];
}
return(str_replace(" ","+",$q));
}
private static function _API() {
$Api = self::$DireccionAPI."method=youtube.videos.list_by_tag&dev_id=".self::$dev_id."&tag=".self::_q()."&page=".(self::_Pag()*20)."&per_page=".self::$TotalResultados;
return($Api);
}
private static function _ExtraerCodigo() {
$url = self::_API();
if(function_exists('file_get_contents')) $html = @file_get_contents($url);
else $html = @join('',file($url));
return(str_replace(array("\r","\n"),'',$html)); //str_replace(array("\r","\n"),'',$html);
}
/*
public static function Video() {
$id = self::id($url);
$html = self::_ExtraerCodigoVideo('http://www.youtube.com/watch?v='.$id);
}
*/
public static function _Paginacion($totals){
$TotalPaginas = ceil((($totals > 20)?($totals):(20))/self::$TotalResultados);
$Anterior = (self::_Pag() > 1) ? (" <a class=enlacerojo href='".self::_GET()."pag=".(self::_Pag()-1)."'>« Anterior</a> ") : (" <font class=style6>« Anterior</font> ");
$Siguiente = (self::_Pag() < $TotalPaginas) ? (" <a class=enlacerojo href='".self::_GET()."pag=".(self::_Pag()+1)."'>Siguiente »</a> ") : (" <font class=style6>Siguiente »</font> ");
echo $Anterior;
for($pag = (self::_Pag()-5); $pag <= (self::_Pag()+5); $pag++ ){
if($pag > 0 && $pag <= $TotalPaginas ){
if($pag == self::_Pag()){
echo " <font class=style6>".$pag."</font> ";
}else {
echo " <a class=enlacerojo href='".self::_GET()."pag=".$pag."'>".$pag."</a> ";
}
}
}
echo $Siguiente;
}
public static function VideosYoutube(){
$_Codigo = self::_ExtraerCodigo();
preg_match_all("/<rating_avg>(.*?)<\/rating_avg>/",$_Codigo,$Ranking);
preg_match_all("/<id>(.*?)<\/id>/",$_Codigo,$id);
preg_match_all("/<title>(.*?)<\/title>/",$_Codigo,$Titulo);
preg_match_all("/<length_seconds>([0-9]+)<\/length_seconds>/",$_Codigo,$Duracion);
preg_match("/<total>([0-9]+)<\/total>/",$_Codigo,$Total);
$columna = 0;
self::_Paginacion($Total['1']);
echo '<br />';
echo '<table border="0" width="100%" cellspacing="0" cellpadding="0" ><tr>';
for($i = 0; $i< count($id['0']); $i++){ $columna++;
$seg = $Duracion['1'][$i];
$minutos = number_format($seg/60,00);
$segundos = $seg % 60;
echo '<td width="50%" valign=top style="border-top: 1px dotted #ccc;border-right: 1px dotted #ccc;padding:0px"><br />
<a href="../videosperu/?v='.$id['1'][$i].'&q='.str_replace(" ","+", $_GET['q']).'&pag='.$_GET['pag'].'" class="EstilosClasificac">
<img border=0 width="75" height="50" src="http://img.youtube.com/vi/'.$id['1'][$i].'/1.jpg" alt="'.($Titulo['1'][$i]).'"> <img border=0 width="75" height="50" src="http://img.youtube.com/vi/'.$id['1'][$i].'/2.jpg" alt="'.($Titulo['1'][$i]).'"> <img border=0 width="75" height="50" src="http://img.youtube.com/vi/'.$id['1'][$i].'/3.jpg" alt="'.($Titulo['1'][$i]).'">
</a
<br />
<div align=left class="negromenu" style="padding-left:10px">
<a href="../videosperu/?v='.$id['1'][$i].'&q='.str_replace(" ","+", $_GET['q']).'&pag='.$_GET['pag'].'" class="EstilosClasificac">'.self::_CodificarTexto($Titulo['1'][$i]).'</a>
<br />
<div style="float:left"><b>Duración: </b>'.$minutos.':'.$segundos.' </div>
<div style="margin-left:20px;float:left;height:10px; width:50px; background-image: url(../images/estrellas_plomas.jpg);">
<div style="height:10px; width:'.@($Ranking['1'][$i]*10).'px; background-image: url(../images/estrellas_rojas.jpg);"><img src="" height="1" width="1"></div></div>
</div>
</td>';
if(($columna%2) == 0){echo '</tr><tr>';}
}
echo '</tr></table>'; echo '<br />';
self::_Paginacion($Total['1']);
}
} #
if(!empty($_GET['v'])){
_Buscador_Youtube::Video("http://www.youtube.com/watch?v=".$_GET['v']);
}
$Titulo = ((!empty($_GET['v']))?(_Buscador_Youtube::$Titulo.' - '):(''))."VIDEOS DEL PERU - Peru Avisos .com - El Portal de noticias y Anuncios Clasificados GRATIS mas grande del Peru";
$Descripcion = "El Portal de noticias y Anuncios Clasificados GRATIS mas Grande del Perú, Donde ademas Encontrara Directorio de Empresas Peruanas, Buscador de Paginas del PERU, Peruanos en el Extranjero, Profesionales Peruanos y Noticias Peruanas.";
$Tag = "noticias, rpp noticias, peru21, diario el comercio, peru, avisos clasificados, publicar gratis anuncios clasificados, comercio electronico, empleos peru, automotores, inmobiliarias, alquileres de departamentos, buscadores de paginas peruanas, profesionales, compra y venta de productos y servicios, publica gratis con fotografias y en espaol.";
include_once("../CodigoHTML/HTML.Superior.php");
?>
<br />
<center>
<form method="get" action="">
<input style="width:350px" type="text" name="q">
<input type="submit" value="Buscar video">
</form>
<?php
if(!empty($_GET['v'])){
if(!empty(_Buscador_Youtube::$id)){
//$tags = explode(", ",_Buscador_Youtube::$Tags);
echo '<div style="width:500px">
<center><object width="500" height="344"><param name="movie" value="http://www.youtube.com/v/'._Buscador_Youtube::$id.'&hl=en&fs=1"></param>
<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/'._Buscador_Youtube::$id.'&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="500" height="344"></embed>
</object>
<div align=left style="font: 13px Arial">
<b>Titulo: </b>'.iconv("UTF-8", "ISO-8859-1", _Buscador_Youtube::$Titulo).'<br /><br />
<b>Descripción: </b>'.iconv("UTF-8", "ISO-8859-1", _Buscador_Youtube::$Descripcion).'<br /><br />
<b>Tag: </b>'.iconv("UTF-8", "ISO-8859-1", _Buscador_Youtube::$Tags).'<br />
</div>
</div><br />
';
} else {
echo "<h1 style='font: 14px Arial'><B>VIDEO SOLICITADO NO ENCONTRAD0</B><h2>";
}
}
?>
<center>
<?
_Buscador_Youtube::VideosYoutube();
include_once("../CodigoHTML/HTML.Inferior.php");
?>
![sonriente](http://static.forosdelweb.com/fdwtheme/images/smilies/smile.png)