Question

I want to read and save some data from this rss feed to in my database table. The RSS feed is http://feeds.feedburner.com/TechCrunch/.

I have used the following code before to read another RSS feed:

$homepage = file_get_contents('http://rss.cnn.com/rss/edition_technology.rss');
$homepage = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $homepage);
$xml = simplexml_load_string($homepage,'SimpleXMLElement', LIBXML_NOCDATA);

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

foreach($xml->channel->item as $opt) {
    $title = mysql_real_escape_string($opt->title);
    $link = mysql_real_escape_string($opt->link);
    $des = mysql_real_escape_string($opt->description);
    // and others
    $sql = 
        "INSERT INTO store_feed (title, link, description) 
         VALUES('$title','$link','$des') 
         ON DUPLICATE KEY UPDATE title = '$title', description = '$des'";
    $result = mysql_query($sql) or die( mysql_error() );
}

... and I got the desired data, but this time data is different.

I want to store link, description, image, publish date, title of this feed. How can I do this?

I know how to insert into a database but how do I get this data from the RSS feed? Please, I need guidance.

Was it helpful?

Solution

When you use simplexml_load_string() on an xml string, you convert it to an object tree.

This XML:

<channel>
  <item>
    <title>Example Title</title>
    <description>Example Description</description>
  </item>
</channel>

... is converted to something you can use like so:

$xml->channel->item->title;
$xml->channel->item->description;

So you need to look at XML of the new RSS feed to see how you can change your code. It will probably look something like this:

foreach($xml->channel->item as $opt) {
    $title = mysql_real_escape_string($opt->title);
    $link = mysql_real_escape_string($opt->link);
    $des = mysql_real_escape_string($opt->description);
    $publication_date = mysql_real_escape_string($opt->pubDate);
    $image = mysql_real_escape_string(strip_tags($opt->description, '<img>'));
}

The image is inside the description, so we can extract it using strip_tags().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top