我有一个视频的YouTube视频XML文件,我可以成功提取一些元素(title,viewcount,favoritecount),但奇怪的是有些元素不起作用(内容)。我的问题在哪里?我不知道这段代码是否“高效”,也许有一种更简单的方法可以做到这一点?
<?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/>';
}
?>
此外,我尝试过不同的方法,比如simplexml_load_file,但这根本不起作用?
<?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;
}
?>