PHP and AJAX create advanced RSS aggregator (1) _ PHP Tutorial

Source: Internet
Author: User
PHP and AJAX to create an advanced RSS aggregator (1 ). I. INTRODUCTION it is no longer difficult to develop an RSS aggregator, but it is still quite difficult to develop a high-quality RSS aggregator. On the other hand, create I. INTRODUCTION

Now, developing an RSS aggregator is no longer difficult, but developing a high-quality RSS aggregator is still quite difficult. On the other hand, it is generally not difficult to create a custom aggregator, and you can provide an interface of your choice in this aggregator. RSS aggregation represents a type of data that is especially suitable for consumption by an AJAX application, because it is in XML format and AJAX can display new feedback well without page refreshing. However, the problem always exists: it is impossible to implement cross-origin AJAX requests in a standard AJAX engine. In this article, I will show you how to use a simple PHP function to bridge AJAX engines and remote content (in this article it refers to RSS feedback ).

II. start

Before we officially start, I would like to briefly introduce the AJAX engine we will use to send requests. The engine simplifies AJAX calls and helps eliminate a large number of redundancy when requests are sent and scheduled. I will not discuss its composition code in detail, but will only give you a brief introduction to how we use it in this article.

First, we need to import all the JavaScript files that constitute the engine. The code included in our index.html file looks as follows:

<script type="text/javascript" src="js/model/HTTP.js?1.1.9"></script>
<script type="text/javascript" src="js/model/Ajax.js?1.1.9"></script>
<script type="text/javascript" src="js/model/AjaxUpdater.js?1.1.9"></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 processes our AJAX calls. We simply call its Update method and pass the requested method, the URL of our request, and the callback method we want to delegate the response.

When sending our request, this is all we need to care about. Now, let's focus on customizing the RSS aggregator function.

III. entry point

It refers to the file index.html, which we call from the browser. The following code describes the index:

<html>
<head>
<title>RSS Aggregation with PHP and Ajax</title>
<link href="css/layout.css?1.1.9" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/model/HTTP.js?1.1.9"></script>
<script type="text/javascript" src="js/model/Ajax.js?1.1.9"></script>
<script type="text/javascript" src="js/model/AjaxUpdater.js?1.1.9"></script>
<script type="text/javascript" src="js/controller/Aggregator.js?1.1.9"></script>
<script type="text/javascript" src="js/view/Feed.js?1.1.9"></script>
</head>
<body>

<p id="Aggregator">
<form name="feedForm" method="post" action="javascript:AjaxUpdater.Update('GET',

'bridge/rss.php?feed=' + document.feedForm.feed.value, Aggregator.Read);">
<p class="header">
<input type="text" name="feed" id="feed" size="50">
<input type="submit" name="submit" value="Add Feed">

</p>
</form>
<p class="leftColumn">
<p id="titles"></p>
<p id="loading"></p>
</p>
<p class="rightColumn">
<p id="description"></p>
</p>

</p>

</body>
</html>

This file is responsible for importing and processing the CSS file displayed by our aggregator and all the JavaScript files used to create the aggregator and send AJAX requests.

Tip: This document does not discuss the CSS file. we only focus on the aggregation and analysis through JavaScript.

Then, the index 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 used to enter the URL for RSS feedback and a submit button used to send requests to them. When you click this button, a request is sent to receive RSS feedback and the response is sent to an object called Aggregator; we will introduce it after discussing the use of AJAX technology for remote RSS Feedback retrieval.

4. Cross-Origin AJAX requests

Cross-Origin AJAX requests are impossible, but there are some methods to solve this problem using a server language. In this section, I will discuss how to use PHP to create a bridge between an AJAX request and a remote RSS feed, so as to successfully implement cross-origin requests. I think you may be surprised at its easy implementation.

PHP provides a local method called file_get_contents, which can read the entire file content into a string. If you start the fopen package, this file can be a remote file, which is started by default when you install PHP. If you set allow_url_fopen to off in the php. ini file, it will be disabled. The following code corresponds to the content of the bridge.php file. when submitting a ticket, 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, which is responsible for setting the response content type to a valid XML for our request object. Then, call file_get_contents and combine the feed url-to pass the request from the form in the index.html file. Once the data is ready, the AJAX engine proxies them to the callback method-our Aggregator object.

1

Now, developing an RSS aggregator is no longer difficult, but developing a high-quality RSS aggregator is still quite difficult. On the other hand, create a fixed...

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.