Skip to content

Instantly share code, notes, and snippets.

@marcosfreitas
Last active January 1, 2016 00:59
Show Gist options
  • Save marcosfreitas/da7cce826554d1b66874 to your computer and use it in GitHub Desktop.
Save marcosfreitas/da7cce826554d1b66874 to your computer and use it in GitHub Desktop.
busca atributos dos vídeos do youtube
<?php
header('Content-Type: text/html; charset=ISO-8859-1');
error_reporting(false);
// integração php (simple xml com api do youtube)
// read feed into SimpleXML object
$sxml = simplexml_load_file('http://gdata.youtube.com/feeds/api/users/FoxSportsBrasil/uploads/');
if(!$sxml) echo 'Tente novamente mais tarde.';
// iterate over entries in feed
$thumbnail = array();
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('media',TRUE);
// get video thumbnail
$attrs = $media->group->thumbnail[1]->attributes();
$thumbnail = $attrs['url'];
// varia de acordo com o tipo de feed
//http://i1.ytimg.com/vi/wYtXO-Oq8LU/0.jpg
//https://i.ytimg.com/vi/Pb6SIXlpjnQ/mqdefault.jpg
$watch = str_replace('http://', '', $thumbnail);
//i1.ytimg.com/vi/_brPE8nj1wQ/1.jpg
// divide em 3 partes
$watch = explode('/', $watch);
$watch = $watch[2]; // id do vídeo
#array com a id de vários vídeos
$array_video_attr[] = $watch;
}//fim foreach videos
#var_dump($array_video_attr);
//embaraçando o array de vídeos
shuffle($array_video_attr);
// escolhe posição aleatória dentro dos índices do array, seleciona esse elemento do índice,
// e dá um explode para separar o link do vídeo e a url do thumbnail
#$attrs = explode('###', $array_video_attr[array_rand($array_video_attr)]);
$attrs = $array_video_attr[array_rand($array_video_attr)];
?>
<iframe width="430" height="240" src="//www.youtube.com/embed/<?php echo $attrs; ?>" frameborder="0" allowfullscreen></iframe>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment