PHP and Ajax create advanced RSS aggregators

Source: Internet
Author: User
Tags install php html form php file php and

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:



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:



RSS Aggregation with PHP and Ajax














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.

V. Aggregator objects

The aggregator object is responsible for receiving the response from the Ajax engine. The following code shows the object (an array called feedcollection, which will be used to store all of the feedback objects created by the retrieved feedback), and a method called read (corresponding to the callback method of the request issued from the Index.html form). When the callback occurs, the requested readystate is checked by a custom Ajax object method that uses a string that describes the div element that displays the loaded message as a parameter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.