0

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;
}
?>
rainerbrunotte
  • 907
  • 1
  • 17
  • 37
  • 1
    Check the DOMXpath class: http://stackoverflow.com/a/25571382/2265374 – ThW Nov 05 '14 at 10:25
  • Unable to reporoduce, it does work with content - it's just perhaps worrying you that the nodevalue of the content element is empty? – hakre Nov 05 '14 at 11:19

0 Answers0