I am working on a RSS-reader using as much Javascript as possible. Because of the same-origin-policy I have to request the XML files of the feeds with another solution. First I used the Yahoo! Query Language (YQL) but to be independent I tried to write some PHP code. Although the responses of both solutions are almost the same (with YQL there is some Yahoo data) my solution using PHP does not work.
The YQL solution
function yahoo() {
// request the xml
$.get("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D'http%3A%2F%2Fnews.yahoo.com%2Frss%2Ftopstories'&diagnostics=false", function (data) {
// save the data (title, content, link, date) to the localStorage
$(data).find("item").each(function () {
el = $(this);
if (!el.find("title").text() || !el.find("description").text() || !el.find("link").text() || !el.find("pubDate").text()) {
console.log("errorEntry");
} else {
localStorage.setItem('titleEntry' , el.find("title").text());
localStorage.setItem('contentEntry' , el.find("description").text());
localStorage.setItem('linkEntry' , el.find("link").text());
localStorage.setItem('dateEntry' , el.find("pubDate").text());
}
});
});
}
My solution JavaScript:
function php() {
// request the xml
$.get("rss_request.php?feedUrl=http://news.yahoo.com/rss/topstories", function (data) {
// save the data (title, content, link, date) to the localStorage
$(data).find("item").each(function () {
el = $(this);
if (!el.find("title").text() || !el.find("description").text() || !el.find("link").text() || !el.find("pubDate").text()) {
console.log("errorEntry");
} else {
localStorage.setItem('titleEntry' , el.find("title").text());
localStorage.setItem('contentEntry' , el.find("description").text());
localStorage.setItem('linkEntry' , el.find("link").text());
localStorage.setItem('dateEntry' , el.find("pubDate").text());
localStorage.setItem('typeEntry' , "rss");
}
});
});
}
PHP (rss_request.php):
<?php
$url = $_GET["feedUrl"];
$response = file_get_contents ($url);
echo $response;
?>