Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get RSS feed into php array - possible?

Tags:

php

rss

I want to parse an existing RSS feed from another website with php and then store certain parts of it in a mysql database.

I'm very competent with php and mysql but have never worked with rss feeds before, where should i start?

  1. is there an equivalent to file_get_contents() for getting rss into php?
  2. are rss feeds broken down into xml/microdata or do i need to use regex to grab bits?

cheers!

like image 895
Haroldo Avatar asked Mar 16 '10 14:03

Haroldo


People also ask

How to get RSS feed data in PHP?

In order to read an RSS feed, you can use the file_get_contents() method and SimpleXMLElement() method. Note: The file_get_contents() method functions by reading the content of a file into a string. The SimpleXMLElement() method functions by representing an XML element.

What is RSS feed in PHP?

PHP RSS FeedRSS is the short name of Really Simple Syndication. RSS is like a broadcast system which is used to deliver updated information to multiple receivers. RSS is used to deliver regular changing web contents to the user.

What is RSS feed in laravel?

RSS stands for Really Simple Syndication and is a feed that returns information in XML format. Having an RSS feed would allow your users to track the latest posts on your website easily.

What is feed URL?

An RSS feed is a formatted text document that contains all the important information about your show. It's hosted on a server and (usually) has a public URL/link so anyone can view or access its contents.


1 Answers

Short Version: ( NEW )

demo: http://so.lucafilosofi.com/get-rss-feed-into-php-array-possible/

$feed = 'http://stackoverflow.com/opensearch.xml';
$feed_to_array = (array) simplexml_load_file($feed);
//OR $feed_to_array = (array) new SimpleXmlElement( file_get_contents($feed) );
print_r($feed_to_array);

//output

Array
(
    [ShortName] => Stack Overflow
    [Description] => Search Stack Overflow: Q&A for professional and enthusiast programmers
    [InputEncoding] => UTF-8
    [Image] => http://sstatic.net/stackoverflow/img/favicon.ico
    [Url] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [type] => text/html
                    [method] => get
                    [template] => http://stackoverflow.com/search?q={searchTerms}
                )

        )

)

Long Version: ( OLD )

<?php

$rss_tags = array(  
'title',  
'link',  
'guid',  
'comments',  
'description',  
'pubDate',  
'category',  
);  
$rss_item_tag = 'item';  
$rss_url = 'http://www.webaddict.info/feeds/news.xml';

$rssfeed = rss_to_array($rss_item_tag, $rss_tags, $rss_url);

echo '<pre>';  
print_r($rssfeed);

function rss_to_array($tag, $array, $url) {  
  $doc = new DOMdocument();  
  $doc->load($url);  
  $rss_array = array();  
  $items = array();  
  foreach($doc-> getElementsByTagName($tag) AS $node) {  
    foreach($array AS $key => $value) {  
      $items[$value] = $node->getElementsByTagName($value)->item(0)->nodeValue;  
    }  
    array_push($rss_array, $items);  
  }  
  return $rss_array;  
}  
?>
like image 114
Luca Filosofi Avatar answered Sep 18 '22 19:09

Luca Filosofi