如何使用PHP获取RSS属性

如何使用PHP获取RSS属性,php,dom,rss,Php,Dom,Rss,我有以下代码来解析RSS提要: $doc = new DOMDocument();

我有以下代码来解析RSS提要:

$doc = new DOMDocument();                                                                                                                                                                                                                                                   
$doc->load('http://gdata.youtube.com/feeds/api/users/user/uploads?orderby=viewCount');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('entry') as $node) {
  $itemRSS = array ( 
    'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
    'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
    'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
  );
  array_push($arrFeeds, $itemRSS);
}
提要是这样的:

<title type='text'>Handelswetenschappen</title>
<link rel='alternate' type='text/html' href='http://www.youtube.com/watch?v=eDiF7jVsoAQ&amp;feature=youtube_gdata'/>
<media:group>
  <media:category label='Onderwijs' scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>Education</media:category>
  <media:content url='http://www.youtube.com/v/eDiF7jVsoAQ?version=3&amp;f=user_uploads&amp;app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='253' yt:format='5'/>
  <media:content url='rtsp://v3.cache4.c.youtube.com/CigLENy73wIaHwkEoGw17oU4eBMYDSANFEgGUgx1c2VyX3VwbG9hZHMM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='253' yt:format='1'/>
  <media:content url='rtsp://v6.cache8.c.youtube.com/CigLENy73wIaHwkEoGw17oU4eBMYESARFEgGUgx1c2VyX3VwbG9hZHMM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='253' yt:format='6'/>
  <media:description type='plain'>Wil jij meer weten over de opleiding Handelswetenschappen? Bekijk dan snel dit filmpje. Studenten Lise en Jean-Marie vertellen je alles over Handelswetenschappen studeren. Ze gaan langs bij de decaan, bij een oud-student en bij toekomstig werkgever.</media:description>
  <media:keywords>HUB, hubrussel, hogeschool, universiteit, brussel, unief, studie, student, studeren, studentenleven, kot, examen, les, cursus, prof, docent, diploma, bachelor, master, professioneel, academisch, opleiding, blokken, eindwerk, eindproef, masterproef, stage, studentenclub, cantus, studiebegeleiding, geslaagd, spieken, fuif, uitgaan, job, studentenjob, handelswetenschappen, economie, TW, toegepaste, economische, wetenschappen, ehsal</media:keywords>
  <media:player url='http://www.youtube.com/watch?v=eDiF7jVsoAQ&amp;feature=youtube_gdata_player'/>
  <media:thumbnail url='http://i.ytimg.com/vi/eDiF7jVsoAQ/0.jpg' height='360' width='480' time='00:02:06.500'/>
  <media:thumbnail url='http://i.ytimg.com/vi/eDiF7jVsoAQ/1.jpg' height='90' width='120' time='00:01:03.250'/>
  <media:thumbnail url='http://i.ytimg.com/vi/eDiF7jVsoAQ/2.jpg' height='90' width='120' time='00:02:06.500'/>
  <media:thumbnail url='http://i.ytimg.com/vi/eDiF7jVsoAQ/3.jpg' height='90' width='120' time='00:03:09.750'/>
  <media:title type='plain'>Handelswetenschappen aan de HUB</media:title>
  <yt:duration seconds='253'/>
</media:group>
Handelsweetenschappen
教育类
jij meer weten会在Handelsweetenschapen工作吗?这是一部电影。让-玛丽·维特伦的学生们都比汉德尔斯韦滕·查彭的学生们好。这是一个非常有趣的故事,是一个非常有趣的学生。
HUB,hubrussel,hogeschool,universiteit,brussel,unief,studie,studeren,Studentenleen,kot,examen,les,cursus,prof,docent,文凭,学士,硕士,Professional,Academich,opleiding,blokken,eindwerk,eindproef,masterproef,stage,studentenclub,cantus,Studiebegiding,Geslagd,spieken,fuif,uitgaan,job,studentenjob,HandelsWetenschapen,economie,TW,Toegebaste,economische,Wetenschapen,ehsal
亨德尔斯韦滕斯查潘酒店
通过上面的代码,我能够获取标题和描述。 我需要知道的是如何获取链接,因此实际上是项目“链接”的属性,即媒体:内容url或媒体:播放器url

$link  = $element->getAttribute("href");
所以我想你会想要这样的东西,修改你写的行:

'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
将其更改为:

'link' => $node->getElementsByTagName('link')->getAttribute('href'),

只是一个注释-RSS提要是一个XML文档,因此最好使用SimpleXML或其他XML解析器而不是DOMDocument。我已经看到了两者,为什么一个优于另一个呢?DOM解析器用于解析DOM树,并且具有特定的功能。XML解析器用于解析XML,因此可以帮助您处理自定义名称空间(