31
Classe para Manipular Videos do Youtube via XML
Filed Under (Novas Tecnologias, PHP, Programação) by Alex P. Torres on 31-07-2010
Ola.
venho hoje disponibilizar uma forma de manipular videos do youtube, em seu site.
procurei na internet algo que falasse sobre o assunto mais, é um assunto com poucas informações na internet.
foi simples achei um classe em PHP que trata va os videos do youtube, a classe foi feita por um brasileiro.
vamos ao que interessa.
Youtube.classe.php
<?
class youtube
{
public $url;
public $id;
public function url2id()
{
$aux = explode("?",$this->url);
$aux2 = explode("&",$aux[1]);
foreach($aux2 as $campo => $valor)
{
$aux3 = explode("=",$valor);
if($aux3[0] == 'v') $video = $aux3[1];
}
return $this->id = $video;
}
public function url2id_($url)
{
$aux = explode("?",$url);
$aux2 = explode("&",$aux[1]);
foreach($aux2 as $campo => $valor)
{
$aux3 = explode("=",$valor);
if($aux3[0] == 'v') $video = $aux3[1];
}
return $this->id = $video;
}
public function thumb_url($tamanho=NULL)
{
$tamanho = $tamanho == "maior"?"hq":"";
$this->url2id();
return 'http://i1.ytimg.com/vi/'.$this->id.'/'.$tamanho.'default.jpg';
}
public function thumb($tamanho=NULL)
{
$tamanho = $tamanho == "maior"?"hq":"";
$this->url2id();
return '<img src="http://i1.ytimg.com/vi/'.$this->id.'/'.$tamanho.'default.jpg" border="0" >';
}
public function info()
{
$feedURL = 'http://gdata.youtube.com/feeds/base/videos?q='.$this->id.'&client=ytapi-youtube-search&v=2';
$sxml = simplexml_load_file($feedURL);
foreach ($sxml->entry as $entry)
{
$details = $entry->content;
$info["titulo"] = $entry->title;
}
$details_notags = strip_tags($details);
$texto = explode("From",$details_notags);
$info["descricao"] = $texto[0];
$aux = explode("Views:",$texto[1]);
$aux2 = explode(" ",$aux[1]);
$info["views"] = $aux2[0];
$aux = explode("Time:",$texto[1]);
$aux2 = explode("More",$aux[1]);
$info["tempo"] = $aux2[0];
$imgs = strip_tags($details,'<img>');
$aux = explode("<img",$imgs);
array_shift($aux);
array_shift($aux);
$aux2 = explode("gif\">",$aux[4]);
array_pop($aux);
$aux3 = $aux2[0].'gif">';
$aux[] = $aux3;
$imagens = '';
foreach($aux as $campo => $valor)
{
$imagens .= '<img'.$valor;
}
$info["estrelas"] = $imagens;
return $info;
}
public function busca($palavra)
{
$feedURL = 'http://gdata.youtube.com/feeds/base/videos?q='.$palavra.'&client=ytapi-youtube-search&v=2';
$sxml = simplexml_load_file($feedURL);
$i=0;
foreach ($sxml->entry as $entry)
{
$details = $entry->content;
$info[$i]["titulo"] = $entry->title;
$aux = explode($info[$i]["titulo"],$details);
$aux2 = explode("<a",$aux[0]);
$aux3 = explode('href="',$aux2[1]);
$aux4 = explode('&',$aux3[1]);
$info[$i]["link"] = $aux4[0];
$details_notags = strip_tags($details);
$texto = explode("From",$details_notags);
$info[$i]["descricao"] = $texto[0];
$aux = explode("Views:",$texto[1]);
$aux2 = explode(" ",$aux[1]);
$info[$i]["views"] = $aux2[0];
$aux = explode("Time:",$texto[1]);
$aux2 = explode("More",$aux[1]);
$info[$i]["tempo"] = $aux2[0];
$imgs = strip_tags($details,'<img>');
$aux = explode("<img",$imgs);
array_shift($aux);
array_shift($aux);
$aux2 = explode("gif\">",$aux[4]);
array_pop($aux);
$aux3 = $aux2[0].'gif">';
$aux[] = $aux3;
$imagens = '';
foreach($aux as $campo => $valor)
{
$imagens .= '<img'.$valor;
}
$info[$i]["estrelas"] = $imagens;
$i++;
}
return $info;
}
public function player($width,$height)
{
$this->url2id();
print '<object width="'.$width.'" height="'.$height.'"><param name="movie" value="http://www.youtube.com/v/'.$this->id.'&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$this->id.'&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'.$width.'" height="'.$height.'"></embed></object>';
}
}
?>
Aqui vou disponibilizar os exemplos de como usar.
</pre>
$obj = new youtube;
$obj->url = "http://www.youtube.com/watch?v=PzF98ur3kKU";
$obj->player("480","385");/// Tamanho do video
print '<br>';
print $obj->url2id(); /// ID DO VIDEO
print '<br>';
print $obj->url2id_("http://www.youtube.com/watch?v=PzF98ur3kKU"); /// ID DE UM VIDEO
print '<br>';
print $obj->thumb_url("maior");/// MOSTRA ENDEREÇo da IMAGEM MAIOR
print '<br>';
print $obj->thumb_url(); /// MOSTRA ENDEREÇo da IMAGEM PEQUENA
print '<br>';
print $obj->thumb(); ///mostra IMagem PEQUENA
print '<br>';
print $obj->thumb("maior"); /// mostra iomagem MAIOR
print '<br>';
$info = $obj->info();
print $info["titulo"]; /// mostra titulo
print '<br>';
print $info["descricao"]; /// mostra DESCRIÇÂO
print '<br>';
print $info["views"]; /// mostra VIEWS
print '<br>';
print $info["tempo"]; /// mostra TEMPO
print '<br>';
print $info["estrelas"]; /// mostra ESTRELAS
print '<br>';
$arquivos = $obj->busca("Yupimix");
$listagem = array();
foreach($arquivos as $f) {
if(strpos($f,'.')!==0) {
array_push($listagem, $f);
}
}
#Definimos quantos arquivos exibir por página
$paginas = array_chunk($listagem, 5);
$pagina = (int)$_GET["lista"];
$paginas[$pagina];
//$busca = $obj->busca("Yupimix");
//foreach (array_slice($consulta, 0, 10) as $key => $val) {
foreach($paginas[$pagina] as $campo => $valor)
{
print 'Titulo: '.$valor['titulo'].'<br>';
print 'Link: '.$valor['link'].'<br>';
print 'Descrição : '.$valor['descricao'].'<br>';
print 'Views : '.$valor['views'].'<br>';
print 'Tempo : '.$valor['tempo'].'<br>';
print 'Estrelas : '.$valor['estrelas'].'<br>';
$propriedade = new youtube;
$propriedade->url = $valor['link'];
print $propriedade->thumb().'<br>';
print '--------------<br>';
}
#Montamos os links da paginação
for($i=1; $i< count($paginas)+1; $i++) {
echo "[ <a href=\"?lista=$i\">$i</a> ] ";
}
neste codigo de exemplos eu implante uma paginação.
Se você quiser saber mais informações sobre a classe aqui esta o link


