Writing a MySQL RSS Feed Using PHP
Basically I need to create a PHP document that will read the RSS feed and write the information to the MySQL document. However, I have very little experience in this area and was wondering if anyone could show an example of how to do this? I was thinking about something that triggered MagpieRSS, however the documentation still got me very much in the dark =)
Any help would be greatly appreciated
Not sure exactly what you are targeting, but:
In the Magpie documentation : (See usage examples)
require_once 'rss_fetch.inc';
$url = 'http://magpie.sf.net/samples/imc.1-0.rdf';
$rss = fetch_rss($url);
echo "Site: ", $rss->channel['title'], "<br>";
foreach ($rss->items as $item ) {
$title = $item[title];
$url = $item[link];
echo "<a href=$url>$title</a></li><br>";
}
This will make the RSS feed available for use. You can change something like this:
foreach ($rss->items as $item ) {
$title = $item[title];
$url = $item[link];
mysql_query("INSERT INTO `table` (`id`, `title`, `link`) VALUES (NULL, '$title', '$url')")";
}
This should get you started if nothing else.
source to share
The easiest way is to use cURL
to get the information as XML from the rss-url and then use simplexml
to turn the rss-XML into a passing object. Use Xpath to get the pieces of XML you want to store in the DB. Finally, move the data to the DB.
Example
Sorry, I rushed out the door when I saw your question. I actually wrote a really simple script a week ago to do most of what you are talking about:
//cURL to get RSS as XML
function get_rss($feed_url) {
$feed_request = curl_init($feed_url);
curl_setopt($feed_request, CURLOPT_RETURNTRANSFER, 1);
$feed_xml = curl_exec($feed_request);
curl_close($feed_request);
return $feed_xml;
}
function rss2sql($xml, $sql) {
//simplexml to convert XML to objects
$rss_xml = simplexml_load_string($xml);
//XPath to get an array of items in RSS
$rss_items = $rss_xml -> xpath('//item');
if(!$rss_items) {
die("No Items In RSS Feed!");
}
else {
//Loop through each item, convert to string, insert string to MySQL
foreach($rss_items as $item) {
$item_array = array($item->title,$item->link,$item->guid,$item->description);
$item_sql = "(".implode(","$item_array).")";
$item_sql = $sql -> escape_string($item_sql);
$insert = "INSERT INTO rsstable VALUES('$item_sql');
$sql -> query($insert);
}
}
}
$sql = new mysqli("localhost", "my_user", "my_password", "world");
$rss_url = "http://example.org/rssfeed";
$rss_xml = get_rss($rss_url);
rss2sql($its_rss_alerts_xml, $sql);
source to share
MagpieRSS seems like a good choice. Yes, the documentation could be better, but you have all the examples you need on the first page:
require('rss_fetch.inc');
$rss = fetch_rss($url);
$items = rss->items;
Only with the fact that you have an associative array that can then be manipulated to be inserted into your database.
foreach ($rss->items as $item ) {
$title = $item[title];
$url = $item[link];
mysql_query("INSERT INTO rss_articles ( title, link ) VALUES ( $title, $url );
//Of course, this has no security measures which you should really look into.
}
source to share