我正在尝试使用 PHP 的 SimpleXMLElement 来解析 YouTube 频道源并包含描述。我可以轻松地获取标题和视频网址。
这是我可以得到描述的东西
$channel_id = 'UCtNjkMLQQOX251hjGqimx2w';
$yt_url = 'https://www.youtube.com/feeds/videos.xml?channel_id=';
$xml_str = file_get_contents($yt_url.$channel_id);
$xml = new SimpleXMLElement($xml_str);
$xml->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');
$result = $xml->xpath("//media:description");
foreach($result as $r){
print $r;
print '<br />';
}
来自 YouTube 的原始 xml 看起来像这样。
<entry>
<id>yt:video:OTYFJaT-Glk</id>
<yt:videoId>OTYFJaT-Glk</yt:videoId>
<yt:channelId>UCtNjkMLQQOX251hjGqimx2w</yt:channelId>
<title>Guitar E.R. - Setting up wood shop for O Positive custom speaker cabinets.</title>
<link rel="alternate" href="https://www.youtube.com/watch?v=OTYFJaT-Glk"/>
<author>
<name>Guitar ER</name>
<uri>https://www.youtube.com/channel/UCtNjkMLQQOX251hjGqimx2w</uri>
</author>
<published>2020-10-18T16:38:51+00:00</published>
<updated>2020-10-20T01:04:59+00:00</updated>
<media:group>
<media:title>Guitar E.R. - Setting up wood shop for O Positive custom speaker cabinets.</media:title>
<media:content url="https://www.youtube.com/v/OTYFJaT-Glk?version=3" type="application/x-shockwave-flash" width="640" height="390"/>
<media:thumbnail url="https://i4.ytimg.com/vi/OTYFJaT-Glk/hqdefault.jpg" width="480" height="360"/>
<media:description>In this video Doctor John Moran of Guitar E.R. gives us a glimpse into the new wood shop where he will be building custom guitar and bass cabinets, likely under the name O Positive Cabinets.</media:description>
<media:community>
<media:starRating count="0" average="0.00" min="1" max="5"/>
<media:statistics views="22"/>
</media:community>
</media:group>
</entry>
<entry>
如您所见,标题和 uri 很容易获取,但我在组合代码来获取标题、URI 和描述时遇到了麻烦。
这是我获取标题和 URI 的代码
$xml_str = file_get_contents($yt_url.$channel_id);
$xml = new SimpleXMLElement($xml_str);
foreach($xml->entry as $entry){
print $entry->title;
print '<br />';
print $entry->author->uri;
print '<br />';
}
经过一段时间的研究,我找到了一个解决方案,但不确定这是否是最好的答案。
<?
$channel_id = 'UCtNjkMLQQOX251hjGqimx2w';
$yt_url = 'https://www.youtube.com/feeds/videos.xml?channel_id=';
$xml_str = file_get_contents($yt_url.$channel_id);
$xml = new SimpleXMLElement($xml_str);
$xml->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');
$yt_data = array();
foreach($xml->entry as $entry){
$videoid = (string)$entry->children('yt', true)->videoId;
$yt_data[] = array(
'id' => $videoid,
'title' => (string)$entry->title,
'description' => (string)$entry->children('media', true)->group->description,
'img' => (string)'https://img.youtube.com/vi/'.$videoid.'/maxresdefault.jpg',
'thumb' => (string)'https://img.youtube.com/vi/'.$videoid.'/mqdefault.jpg',
'published' => (string)$entry->published,
'updated' => (string)$entry->updated,
'channel' => (string)$entry->children('yt', true)->channelId,
'author' => (string)$entry->author->name,
'uri' => (string)$entry->author->uri,
'views' => (string)$entry->children('media', true)->group->community->statistics->attributes()['views'],
'ratings_count' => (string)$entry->children('media', true)->group->community->starRating->attributes()['count'],
'ratings_avg' => (string)$entry->children('media', true)->group->community->starRating->attributes()['average'],
);
}
foreach($yt_data as $yt){
print '<div class="ytVidWrap">';
print '<a href="https://www.youtube.com/watch?v='.$yt['id'].'" target="_blank">';
print '<img src="'.$yt['thumb'].'" alt="YouTube Video" />';
print '<h1>'.$yt['title'].'</h1>';
print '<p>'.$yt['description'].'</p>';
print '<p>'.$yt['views'].' views • '.date('M j, Y',strtotime($yt['published'])).'</p>';
print '</a>';
print '</div>';
}
?>
您的代码似乎采用作者频道 ID 的 URL,每个视频都相同,而不是每个视频的视频标题都不同。
有一个解析器库:
https://github.com/gbuckingham89/youtube-rss-parser/blob/master/src/Parser.php#L139
阅读代码:
$video->id = (string) $video_xml->children('http://www.youtube.com/xml/schemas/2015')->videoId;
$video->url = (string) $video_xml->link->attributes()->href;
$video->title = (string) $video_xml->title;
$video->description = (string) $video_xml->children('http://search.yahoo.com/mrss/')->group->description;