What Rss Parser Should I Use in PHP

What RSS parser should I use in PHP?

My defacto answer will be "have you tried SimplePie?", it's a very good XML parser but you'll have to have a look at their demo to see how it handles broken feeds :-)

Best way to parse RSS/Atom feeds with PHP

Your other options include:

  • SimplePie
  • Last RSS
  • PHP Universal Feed Parser

PHP - RSS Parser XML

I came up with this:

<?php
$url = "http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml"; // xmld.xml contains above data
$feeds = file_get_contents($url);
$rss = simplexml_load_string($feeds);

$items = [];

foreach($rss->channel->item as $entry) {
$image = '';
$image = 'N/A';
$description = 'N/A';
foreach ($entry->children('media', true) as $k => $v) {
$attributes = $v->attributes();

if ($k == 'content') {
if (property_exists($attributes, 'url')) {
$image = $attributes->url;
}
}
if ($k == 'description') {
$description = $v;
}
}

$items[] = [
'link' => $entry->link,
'title' => $entry->title,
'image' => $image,
'description' => $description,
];
}

print_r($items);
?>

Giving:

Array
(
[0] => Array
(
[link] => SimpleXMLElement Object
(
[0] => https://www.nytimes.com/2017/04/17/sports/basketball/a-court-used-for-playing-hoops-since-1893-where-paris.html?partner=rss&emc=rss
)

[title] => SimpleXMLElement Object
(
[0] => A Court Used for Playing Hoops Since 1893. Where? Paris.
)

[image] => SimpleXMLElement Object
(
[0] => https://static01.nyt.com/images/2017/04/05/sports/basketball/05oldcourt10/05oldcourt10-moth-v13.jpg
)

[description] => SimpleXMLElement Object
(
[0] => The Y.M.C.A. in Paris says its basketball court, with its herringbone pattern and loose slats, is the oldest one in the world. It has been continuously functional since the building opened in 1893.
)

)
.....

And you can iterate over

foreach ($items as $item) {
printf('<img src="%s">', $item['image']);
printf('<a href="%s">%s</a>', $item['url'], $item['title']);
}

Hope this helps.

What is the Fastest Way to parse RSS feed in PHP?

It seems you are parsing your feeds online, on each and every user request.

It always would be slow. It is network, you know.

The only sensible way of reading RSS feeds is asynchronous one. One script to parse them regularily and update the local database and another to show entries from the local database.

And, you know, gzipping output has very little to do with parsing inbound data. Go figure.

what is the best php library to parse RSS feeds?

Here is a good overview: http://www.webresourcesdepot.com/php-rss-parsers/

RSS Parser to include Categories

I would suggest using SimpleXML to parse the feed.

Here is how you can do it:

$feed_url = 'http://o7thblog.com/feed/';
$feed = simplexml_load_file($feed_url, null, LIBXML_NOCDATA);
$channel = $feed->channel;
echo "<h1><a href=\"{$channel->link}\">{$channel->title}</a></h1>\n";
echo "{$channel->description}\n";
echo "<dl>\n";
foreach ($channel->item as $item) {
echo "<dt><a href=\"{$item->link}\">{$item->title}</a></dt>\n"
. "<dd style=\"margin-bottom: 30px;\"><div style=\"font-size: small;\">{$item->pubDate}</div>\n"
. "<div>{$item->description}</div>\n"
. "Categories: <strong>".implode('</strong>, <strong>', (array) $item->category) . "</strong>\n</dd>";
}
echo "</dl>\n";

Above shows you all categories.

Parsing an RSS feed in PHP with DOM

DOMDocument could not get the 'channel' object after parsing. Here's the GetFeed() function using simpleXML:

test.php

    <?php
function GetFeed($url){
$feed = simplexml_load_file($url);
$feed_array = array();
foreach($feed->channel->item as $story){
$story_array = array (
'title' => $story->title,
'desc' => $story->description,
'link' => $story->link,
'date' => $story->date
);

array_push($feed_array, $story_array);
}

return $feed_array;
}
?>

Hope it helps. Your index.php will remain same.

What file extension should I use for dynamic RSS

With the AddType application/x-httpd-php .xml line, Apache will be able to serve XML files containing some PHP code inside. So you can save this file as a .xml, the PHP code will be interpreted

Perhaps you should add a cache manager in order to avoid generating the same feed when there is no new article?

php: parsing rss feed using simplexml and xpath

$imgs=$xml->xPath('//channel/item/enclosure');
foreach($imgs as $img) {
var_dump($img->attributes());
}


Related Topics



Leave a reply



Submit