如何用PHP解析YouTube XML,包括名称空间描述
我试图使用PHP的SimpleXMLElement解析YouTube频道提要并包含描述。我可以很容易地获得标题和视频url 这是我能得到描述的东西如何用PHP解析YouTube XML,包括名称空间描述,php,xml,parsing,youtube,feed,Php,Xml,Parsing,Youtube,Feed,我试图使用PHP的SimpleXMLElement解析YouTube频道提要并包含描述。我可以很容易地获得标题和视频url 这是我能得到描述的东西 $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 Simple
$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 />';
}
$channel_id='UCtNjkMLQQOX251hjGqimx2w';
$yt_https://www.youtube.com/feeds/videos.xml?channel_id=';
$xml\u str=file\u get\u contents($yt\u url.$channel\u id);
$xml=新的simplexmlement($xml\u str);
$xml->registerXPathNamespace('前缀','http://www.w3.org/2005/Atom');
$result=$xml->xpath(//media:description”);
foreach(结果为$r){
打印$r;
打印“
”;
}
来自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>
yt:视频:OTYFJaT Glk
奥蒂夫贾特Glk
UCTNJKMLQOX251HJGQIMX2W
吉他E.R.-为O-Positive定制音箱柜建立木材车间。
';
打印“”;
}
?>
$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>';
}
?>