Best Way to Parse Rss/Atom Feeds With PHP

Best way to parse RSS/Atom feeds with PHP

Your other options include:

  • SimplePie
  • Last RSS
  • PHP Universal Feed Parser

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.

PHP Parsing RSS Feed

$pos = 0
while($x = strpos($data, '<media:content url="', $pos) !== FALSE)
{
$y = strpos($data, '" type="', $pos)
$imgurl = substr($data, $x+'num of chars in <media:content url="', $y);
$pos = $x
//write imgurl to whatever file you like
}

untested code I am sure there are syntax issues with it but should give you want you need to do it if you do not want to use an xml parser thats already been made

what that is going for is iterating through $data starting at $pos if it finds an instance of

Once it has both those positions itll substr and you need to pull from $data $x(start position of media:content)+ number of chars in your search param and $y start position of the end of the url...

edit- apparently it do didnt like the $x in there like that so

$url = 'http://www.actionsportsinc.com/p200285048/recent.rss';

$data = file_get_contents( $url );
$pos = 0;
while(strpos($data, '<media:content url="', $pos) !== FALSE)
{
$x = strpos($data, '<media:content url="', $pos) ;
$y = strpos($data, ' type="image', $x);
$imgurl = substr($data, $x+20, $y - 49 - $x);
$pos = $x + 1;
echo $imgurl . "|| <br>";
//write imgurl to whatever file you like
}

That is the exact code pasted straight from my npp

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.

How to use a php script to grab rss feed content

SO is for asking specific questions related to programming. Even though your question is related to programming you are not asking a specific question.

A quick google search for "PHP read RSS feeds gives you a list of very good links which can get you started.

How to Read an RSS Feed With PHP – screencast

Try out the example and see if it fits your requirement. If you have any specific questions then come back to SO and I am sure everyone will be glad to help.

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.

How can I parse RSS description tag with PHP

I used LastRSS built in function to strip out the CDATA tags and keep the rest of the html attributes in tact.

This is done simply as follows:

// Create lastRSS object
$rss = new lastRSS;

$rss->CDATA = 'content';


Related Topics



Leave a reply



Submit