gpt4 book ai didi

php - PHP SimpleXML-从Youtube XML Feed获取数据

转载 作者:行者123 更新时间:2023-12-03 05:57:05 25 4
gpt4 key购买 nike

我正在尝试从此Youtube XML文件中提取数据-http://www.youtube.com/feeds/videos.xml?user=latenight

通过使用此代码,我可以轻松掌握

<id>yt:video:dLUfTS4TxrQ</id>
<title>Sia: Cheap Thrills</title>
<published>2016-01-28T20:04:03+00:00</published>


$feed = simplexml_load_file("http://www.youtube.com/feeds/videos.xml?user=latenight");

echo $feed->entry[0]->title;
echo $feed->entry[0]->id;
echo $feed->entry[0]->published;

您如何提取其中的数据
媒体:缩略图,媒体:描述,媒体:starRating,媒体:统计是否使用PHP SimpleXML?
<media:group>

<media:title>Sia: Cheap Thrills</media:title>
<media:content url="https://www.youtube.com/v/dLUfTS4TxrQ?version=3" type="application/x-shockwave-flash" width="640" height="390"/>
<media:thumbnail url="https://i1.ytimg.com/vi/dLUfTS4TxrQ/hqdefault.jpg" width="480" height="360"/>
<media:description>
Music guest Sia performs "Cheap Thrills" for the Tonight Show audience. Subscribe NOW to The Tonight Show Starring Jimmy Fallon: http://bit.ly/1nwT1aN Watch The Tonight Show Starring Jimmy Fallon Weeknights 11:35/10:35c Get more Jimmy Fallon: Follow Jimmy: http://Twitter.com/JimmyFallon Like Jimmy: https://Facebook.com/JimmyFallon Get more The Tonight Show Starring Jimmy Fallon: Follow The Tonight Show: http://Twitter.com/FallonTonight Like The Tonight Show: https://Facebook.com/FallonTonight The Tonight Show Tumblr: http://fallontonight.tumblr.com/ Get more NBC: NBC YouTube: http://bit.ly/1dM1qBH Like NBC: http://Facebook.com/NBC Follow NBC: http://Twitter.com/NBC NBC Tumblr: http://nbctv.tumblr.com/ NBC Google+: https://plus.google.com/+NBC/posts The Tonight Show Starring Jimmy Fallon features hilarious highlights from the show including: comedy sketches, music parodies, celebrity interviews, ridiculous games, and, of course, Jimmy's Thank You Notes and hashtags! You'll also find behind the scenes videos and other great web exclusives. Sia: Cheap Thrills http://www.youtube.com/fallontonight
</media:description>

<media:community>
<media:starRating count="16185" average="4.86" min="1" max="5"/>
<media:statistics views="648221"/>
</media:community>

</media:group>

最佳答案

我不确定如何使用SimpleXML进行操作-使用标准DOMDocument非常简单〜如果您想直接定位特定节点,则可以使用XPath查询。

以下内容使用命名空间media定位以media为前缀的所有节点-然后遍历集合。可以通过其他方式完成此操作,但希望它将对入门有所帮助?!

$url='https://www.youtube.com/feeds/videos.xml?user=latenight';
/* create DOMDocument object*/
$dom=new DOMDocument;

/* load external file directly */
$dom->load( $url );

/* to get all elements based upon namespace and iterate through */
$col=$dom->getElementsByTagNameNS('http://search.yahoo.com/mrss/','*');
/* if the above failed, do nothing or display a message perhaps */
if( $col ){
/* loop through entire collection */
foreach( $col as $node ){

/* this shows pretty much everything apart from node attributes which you probably want to collect / process */
#echo 'Prefix:'.$node->prefix .' Tag:'.$node->tagName.' Value:'.$node->nodeValue.BR;

if( $node->tagName==$node->prefix.':description' ) echo 'DESCRIPTION:'.$node->nodeValue;

if( $node->attributes->length > 0 ) {
if( $node->tagName==$node->prefix.':starRating' && $node->hasAttribute('count') ) echo 'COUNT:'.$node->getAttribute('count');
if( $node->tagName==$node->prefix.':starRating' && $node->hasAttribute('average') ) echo 'AVERAGE:'.$node->getAttribute('average');
if( $node->tagName==$node->prefix.':statistics' && $node->hasAttribute('views') ) echo 'STATISTICS:'.$node->getAttribute('views');
/* etc */
}
}
}

关于php - PHP SimpleXML-从Youtube XML Feed获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35158388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com