Skip to content

Instantly share code, notes, and snippets.

@erfg12
Last active July 6, 2022 20:45
Show Gist options
  • Save erfg12/2225545db3651d0f262190cb8c5651c4 to your computer and use it in GitHub Desktop.
Save erfg12/2225545db3651d0f262190cb8c5651c4 to your computer and use it in GitHub Desktop.
RSS --> JS (CORS bypass)
<body>
<!-- host this on GitHub.com pages for free -->
<div id="DisplayData"></div>
<script>
fetch('https://cnet-rss-cors.herokuapp.com/')
.then(response => response.text())
.then(data => {
parser = new DOMParser();
xmlDoc = parser.parseFromString(data, "text/xml");
Array.from(xmlDoc.getElementsByTagName("item")).forEach(element => {
document.getElementById("DisplayData").innerHTML += "<p><a href=\"" + element.getElementsByTagName("link")[0].innerHTML + "\"><img src=\"" + element.getElementsByTagName("media:thumbnail")[0].getAttribute('url') + "\"/><br><h3>" + element.getElementsByTagName("title")[0].innerHTML + "</h3>" + element.getElementsByTagName("description")[0].innerHTML + "</p>";
console.log(element);
});
});
</script>
</body>
<?php
// host this on Heroku.com for free
libxml_use_internal_errors(true);
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/xml; charset=UTF-8");
error_reporting(E_ALL);
function GetURLData($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$head = curl_exec($ch);
curl_close($ch);
return $head;
}
print_r(GetURLData("https://www.cnet.com/rss/news/"));
?>
@erfg12
Copy link
Author

erfg12 commented Jul 6, 2022

If you want to do this for free, host the PHP script on Heroku and plug the URL into the client file. If you wanna host the JavaScript client somewhere for free, you can always use GitHub pages.

Want a list of great RSS feeds? Check this out! https://about.fb.com/wp-content/uploads/2016/05/rss-urls-1.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment