I have a YouTube video XML file for a video, I can extract a few elements successfully (title, viewcount, favoritecount), but strangely for some it does not work (content). Where is my problem? I don't know if this code is "efficient" too, maybe there is a simpler way of doing this?
<?php
$id = 'iRBFn59CX4U';
$xml_data = @file_get_contents("http://gdata.youtube.com/feeds/api/videos/$id?v=2");
$doc = new DOMDocument();
$doc->loadXML($xml_data);
$titles = $doc->getElementsByTagName("title");
foreach ($titles as $title) {
$title = $title->nodeValue;
echo '<h1>'.$title.'</h1>';
}
$contents = $doc->getElementsByTagName("content");
foreach ($contents as $content) {
$content = $content->nodeValue;
echo '<p>'
.$content.'</p>';
}
$stats = $doc->getElementsByTagNameNS("*","statistics");
foreach ($stats as $stat) {
$views = $stat->getAttribute('viewCount');
echo 'Views: '.$views.'<br/>';
}
$stats = $doc->getElementsByTagNameNS("*","statistics");
foreach ($stats as $stat) {
$faves = $stat->getAttribute('favoriteCount');
echo 'Faves: '.$faves.'<br/>';
}
?>
Furthermore I have tried different methods like simplexml_load_file but that doesn't work at all somehow?
<?php
$id = 'iRBFn59CX4U';
$data = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos/$id?v=2");
foreach ($data as $xml) {
$title = $xml->entry->title;
echo $title;
}
?>