-1

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.

Gustav Bertram
  • 14,591
  • 3
  • 40
  • 65
omnath
  • 509
  • 8
  • 25
  • You have added NO information about your database(MySQL, MSSQL, Oracle) and if you have a database already. Some more information please. – Manuel Nov 09 '11 at 09:02
  • I am using mysql and I don't have any issue for insert in my database,but i want to know how I read this data from rss feed. – omnath Nov 09 '11 at 09:05
  • dragon112, it is clear from the code that he already has a database connection, and an underlying database and that he is using MySQL. – Gustav Bertram Nov 09 '11 at 09:11
  • what is your question? how to use simplexml? – Gordon Nov 09 '11 at 09:24
  • sir, how to read this rss feed http://feeds.feedburner.com/TechCrunch/ – omnath Nov 09 '11 at 09:31

1 Answers1

1

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().

Gustav Bertram
  • 14,591
  • 3
  • 40
  • 65
  • I use your code and Its working for Title, Link, Description but the image is still not working. It saved in Description column but even with "strip_tags()" I cant save it. – Arif May 29 '13 at 09:51