1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| <?php
$feedURL = 'http://gdata.youtube.com/feeds/api/videos/b2GZ99beADQ';
//Après je récupère un fichier xml :
$entry = simplexml_load_file($feedURL);
// function to parse a video <entry>
function parseVideoEntry($entry) {
$obj= new stdClass;
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
$obj->title = $media->group->title;
$obj->description = $media->group->description;
// get video player URL
$attrs = $media->group->player->attributes();
$obj->watchURL = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$obj->thumbnailURL = $attrs['url'];
// get <yt:duration> node for video length
$yt = $media->children('http://gdata.youtube.com/feeds/api/videos/b2GZ99beADQ');
$attrs = $yt->duration->attributes();
$obj->length = $attrs['seconds'];
// get <yt:stats> node for viewer statistics
$yt = $entry->children('http://gdata.youtube.com/feeds/api/videos/b2GZ99beADQ');
$attrs = $yt->statistics->attributes();
$obj->viewCount = $attrs['viewCount'];
// get feed URL for video responses
$entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
$nodeset = $entry->xpath("feed:link[@rel='http://gdata.youtube.com/feeds/api/videos/b2GZ99beADQ']");
if (count($nodeset) > 0) {
$obj->responsesURL = $nodeset[0]['href'];
}
// get feed URL for related videos
$entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
$nodeset = $entry->xpath("http://gdata.youtube.com/feeds/api/videos/b2GZ99beADQ']");
if (count($nodeset) > 0) {
$obj->relatedURL = $nodeset[0]['href'];
}
// return object to caller
return $obj;
}
// set video data feed URL
$vid = "b2GZ99beADQ";
$feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $vid;
// read feed into SimpleXML object
$entry = simplexml_load_file($feedURL);
// parse video entry
$video = parseVideoEntry($entry);
echo "<table>\n";
echo "<tr>\n";
echo "<td><a href=\"{$video->watchURL}\">
<img src=\"$video->thumbnailURL\"/></a></td>\n";
echo "<td><a href=\"{$video->watchURL}\">{$video->title}</a>
<br/>\n";
echo sprintf("%0.2f", $video->length/60) . " min. | {$video->rating}
user rating | {$video->viewCount} views<br/>\n";
echo $video->description . "</td>\n";
echo "</tr>\n";
?> |
Partager