简体   繁体   中英

Parse RSS/Atom feeds with PHP's HTML DomDocument

How to find the values for namespace content:encoded and dc:creator with the following code Unfortunately I cannot use simplepie or magpierss or even simplexml. I know I've to use $doc->getElementsByTagName, but cannot figure out where?

<?php function rss_to_array($tags, $array, $url) { $doc = new DOMdocument(); @$doc->load($url); $rss_array = array(); foreach($tags as $tag) { if ($doc->getElementsByTagName($tag)) { foreach($doc->getElementsByTagName($tag) AS $node) { $items = array(); foreach($array AS $key => $values) { $items[$key] = array(); foreach($values as $value) { if ($itemsCheck = $node->getElementsByTagName($value)) { for( $j=0 ; $j < $itemsCheck->length; $j++ ) { if (($attribute = $itemsCheck->item($j)->nodeValue) != "") { $items[$key][] = $attribute; } else if ($attribute = $itemsCheck->item($j)->getAttribute('term')) { $items[$key][] = $attribute; } else if ($itemsCheck->item($j)->getAttribute('rel') == 'alternate') { $items[$key][] = $itemsCheck->item($j)->getAttribute('href'); } } } } } array_push($rss_array, $items); } } } return $rss_array; } $rss_item_tags = array('item', 'entry'); $rss_tags = array( 'title' => array('title'), 'description' => array('description', 'content', 'summary'), 'link' => array('link', 'feedburner'), 'category' => array('category') ); $rssfeed = rss_to_array($rss_item_tags, $rss_tags, $url); echo '<pre>'; print_r($rssfeed); echo '</pre>'; exit; ?>

for RSS feeds, try using simplexml_load_file. It creates an object out of the XML and, as all RSS feeds are the same, then you can do something like:

$feed = simplexml_load_file(your_rss_url_here);

for($i=0; $i < 10; $i++){
// this is assuming there are 10 pieces of content for each RSS you're loading

    $link = $feed->channel->item[$i]->link;
    // do each for pubdate, author, description, title, etc.
}

http://php.net/manual/en/book.simplexml.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM