2012-02-03 12 views
1

とRSSの属性を取得するために私はRSSフィードを解析するために、次のコードを持っている:どのようにPHP

$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> 

、私はタイトルと説明を取得できます。 私が知る必要があるのは、リンクを取得する方法です。実際には、アイテム 'link'の属性、media:content urlまたはmedia:player urlです。

+0

だけコメント - RSSをフィードはXMLドキュメントなので、DOMDocumentの代わりにSimpleXMLや他のXMLパーサを使用する方がよいでしょう。 – Aurimas

+0

私は両方見たことがあります、なぜ他のものよりも優れていますか? – samn

+0

DOMパーザはDOMツリーを解析するためのもので、そのための特別な機能を持っています。 XMLパーサはXMLを解析するためのもので、カスタムネームスペース( Aurimas

答えて

2
$link = $element->getAttribute("href"); 

だから私はあなたが書いたライン修正し、あなたがこのような何かをしたいだろうと思います。これに

'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 

変更を:

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

あなたの解決策にアイテム(0)を追加することは、トリック、たくさんのことでした! – samn

+0

優秀、よろしくお願いします! – IsisCode