RSS aggregators are an application that is particularly suited for building with a standard AJAX engine, but it is often difficult to implement a Cross-domain Ajax request for RSS feeds. In this article, I'll show you how to use a simple PHP function to implement the "bridging" Ajax engine and RSS content.
First, the introduction
Now, developing an RSS aggregator is no longer a difficult task, but developing a high-quality RSS aggregator is still quite difficult. On the other hand, creating a custom aggregator is generally not difficult, and within this aggregator you can provide an interface of your choice. RSS aggregation represents a class of data that is particularly suited to the consumption of an AJAX application because it is in XML format and Ajax is able to display new feedback well without having to refresh the page. However, the problem always exists: it is impossible to implement Cross-domain AJAX requests in a standard AJAX engine. In this article, I'll show you how to use a very simple PHP function to bridge the AJAX engine and remote content (which in this article refers to RSS feeds).
"Hint" This article assumes that you have a basic understanding of PHP and have experience using AJAX and parsing XML. To fully understand the examples provided in this article, you need to download the corresponding source files.
Second, start
Before we begin formally, I would like to briefly describe the Ajax engine we will use to make the request. The engine simplifies Ajax invocation and helps to eliminate the large amount of redundancy that exists when the request is made and the response is dispatched. I'm not going to discuss its constituent code in detail, and I'll just give you a brief description of how we use it in this article.
First, we need to import all of the JavaScript files that make up the engine. The code contained in our index.html file looks like the following:
<script type="text/javascript" src="js/model/HTTP.js"></script>
<script type="text/javascript" src="js/model/Ajax.js"></script>
<script type="text/javascript" src="js/model/AjaxUpdater.js"></script>
Once we import the JavaScript file, we can send a request by writing code similar to the following:
AjaxUpdater.Update('GET', 'url',callbackMethod);">
The Ajaxupdater is an object that handles our Ajax calls. We simply call its Update method and pass the requested method, the URL we request, and the callback method we want to delegate the response to.
That's all we need to be concerned about when we make a request. Now, let's focus on customizing the RSS aggregator's capabilities.
Third, entry point
The entry point to the aggregator is the index.html file, which we call from the browser. Here is the code that describes the index:
<title> RSS Aggregation with PHP and Ajax </title>
<link href= "Css/layout.css" rel= "stylesheet" type= "Text/css"/>
<script type= "Text/javascript" src= "js/model/http.js" > </script>
<script type= "Text/javascript" src= "js/model/ajax.js" > </script>
<script type= "Text/javascript" src= "js/model/ajaxupdater.js" > </script>
<script type= "Text/javascript" src= "js/controller/aggregator.js" > </script>
<script type= "Text/javascript" src= "js/view/feed.js" > </script>
<body>
<div id= "Aggregator"
<form name= "Feedform" method= "post" action= "javascript:AjaxUpdater.Update" (' Get ', ' bridge/rss.php?feed= ' + Document.feedForm.feed.value, Aggregator.read); " >
<div class= "header" >
<input type= "text" name= "feed" id= "feed" size= "50"
<input type= "Submit" name= "Submit" value= "Add Feed"
</div>
</form>
<div class= "Leftcolumn"
<div id= "titles" > </div>
<div id= "Loading" > </div>
</div>
<div class= "Rightcolumn"
<div id= "description" > </div>
</div>
</div>
</body>
This file is responsible for importing CSS files that handle our aggregator display and all JavaScript files for creating the aggregator and making Ajax requests.
"Prompt" This article does not discuss the CSS file; we focus on the aggregation and analysis implemented through JavaScript.
The index then defines the DIV tag, which will be used to describe the layout of the received data. It also contains a form with an input field to enter the URL for the RSS feed, and a submit button to send them a request. When the button is clicked, a request is sent to receive the RSS feed back and the response is sent to an object called aggregator; we'll introduce it later when we discuss the use of AJAX technology for remote RSS feedback retrieval.
Iv. cross-Domain AJAX requests
Cross-domain AJAX requests are not possible, but there are some ways to use a server-side language to solve this problem. In this section, I'll discuss how to use PHP to create a bridge between AJAX requests and a remote RSS feed, and then achieve the goal of successfully cross-domain requests. I think you'll probably be amazed at how easy it is to achieve.
PHP provides a local method called File_get_contents that can read the entire contents of a file into a string. If you start the fopen wrapper, this file can be a remote file, which is started by default when you install PHP. If you set the Allow_url_fopen to off in the php.ini file, it is not in a prohibited state. The following code corresponds to the contents of the bridge.php file, and when we submit the form we use index.html to send the request:
<?
header("Content-Type: application/xml; charset=UTF-8");
echo file_get_contents($_GET['feed']);
?>
The first line in the code above is a header (header), which is responsible for setting the content type of the response to valid XML for our Request object. Then, call file_get_contents and combine the feedback url-this is passed through our request from the form within the index.html file. Once this data is ready, the Ajax engine proxies them to the callback method-our aggregator object.