Create RSS aggregator based on PHP and AJAX

Source: Internet
Author: User

Imagine using a simple HTML file to send a request to a server script, and receiving a custom xml file based on the request (the standardization is getting closer and closer, then display it to the user without refreshing the browser! The author will discuss with you how to combine php (as the mainstream development language) and ajax (static dynamic website) in common Web applications) technology to create real-time data transmission without refreshing the browser.

Although php (as the mainstream development language) is used in this article, remember that any server-side language will work normally. To understand this article, I assume that you basically understand JavaScript and php (as the mainstream development language) or a server-side language.

In this example, ajax (static dynamic website) is used to send a request from an RSS feed to a custom php (as a mainstream development language) object. The php (as the mainstream development language) object copies the feed on the local server and returns this path. The request object receives the path, analyzes it, and displays the data to the user in HTML format. This sounds like it involves many steps. In fact, it consists of only four small files. Four small files are used to balance their specific strengths and make the processing of the entire system efficient.

Some readers may ask why you want to create a copy of the feed on the local server instead of simply analyzing the original feed. This allows attackers to bypass the cross-origin restrictions imposed by xml (standardization is getting closer and closer) HTTP Request objects. Later, I will explain how to create this custom php (as the mainstream development language) object. But first, let's start with form creation.

Create a Request Form

The first thing you need to do is include JavaScript and any CSS files that you may want to use between your HTML head tags. I included a style table to implement the final layout of the aggregator and used a JavaScript file to send requests and analyze the feeds:

<Link href = "css/layout.css" rel = "stylesheet" type = "text/css"/>
<Script src = "js/request. js"> </script>

Next, create a form that sends a request to an RSS feed you selected. The Form I created contains only one input field and one button for submitting the request. The query of this request is a string consisting of the feed input value and a password that will be verified on the server side. As an example, I use the following form:
"Password = mypassword

This code sends a request each time the page is loaded. Therefore, if the page is refreshed, the existing feed string in this input field will be requested during page loading. The following is an example of form data, along with some div labels used to display specific nodes of the analyzed feed:

<Body onload = "javascript: makeRequest (request. php (as the mainstream development language )? Request = + document. feedForm. feed. value + "password = mypassword);">
<Form name = "feedForm" method = "post" action = "javascript: makeRequest (request. php (as the mainstream development language )? Request = + document. feedForm. feed. value + "password = mypassword);">
Enter a feed: <input type = "text" name = "feed" id = "feed" size = "20">
<Input type = "submit" name = "submit" value = "Add Feed">
</Form>
<Div id = "logo"> </div>
<Hr/>
<Div id = "copy"> </div>
<Div id = "details"> </div>
</Body>

The three div labels I created are logo, copy, and details. Each of them has an associated style in the layout style sheet. They will be used when we analyze the feeds, but we need to first be able to access the feeds we requested. This can be done using the php (as the mainstream development language) object I mentioned earlier.

Create a custom php (as the mainstream development language) object

I used php (as the mainstream development language) to create a small RSS class, which creates a copy of the request feed on the local server, in this way, it can be accessed by the xml (standardization is getting closer and closer) HTTP Request object we will create later. Typically, you cannot request a file across domains, which means that the file to be requested must be on the local server. This class is a way to solve cross-domain problems, because it creates a copy of the feed, this copy is requested on the local server and returns the local path to the feed, then it is accessed by the Request object.

The only method in this class is a request method, which has only one parameter pointing to the requested RSS feed URL. Then, it checks whether a directory is located on the local server through the rss name. If it does not exist, create a directory and set its permission mode to 0666, which means the directory can be read and written. When it is set to readable, the directory can be accessed later. When it is set to writable, you can write a copy of the feed to the directory on the local server:

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.