PHP and Ajax Create advanced RSS aggregators (1) _php tutorial

Source: Internet
Author: User
First, 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 can provide an interface of your own choosing within this aggregator. RSS aggregation represents a class of data that is particularly appropriate for an AJAX application because it is in XML format and Ajax is good at displaying new feedback without having to refresh the page. However, the problem always exists: it is not possible 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).

Second, start

Before we begin, I would like to briefly introduce the AJAX engine we will use to make the request. The engine simplifies Ajax calls and helps to eliminate the large amount of redundancy that exists when requesting and dispatching responses are made. I will not discuss its constituent code in detail, but only give you a brief description of how we use it in this article.

First, we need to import all the JavaScript files that make up the engine. The code contained in our index.html file looks like this:



Once we import the JavaScript file, we can make a request by writing code similar to the following:

Ajaxupdater.update (' GET ', ' url ', callbackmethod); >

The Ajaxupdater is an object that is responsible for handling our AJAX calls. We simply call its Update method and pass the requested method, the URL we requested, and the callback method we want to proxy the response to.

That's all we need to be concerned about when we make our request. Now, let's focus on customizing the RSS aggregator's functionality.

Third, the entry point

The entry point to the aggregator is the index.html file, which we call from the browser. The following is the code that describes the index:



RSS Aggregation with PHP and Ajax








' bridge/rss.php?feed= ' + document.feedForm.feed.value, aggregator.read); " >












This file is responsible for importing the CSS file that handles our aggregator display and all the JavaScript files used to create the aggregator and make Ajax requests.

"Hint" This article does not discuss the CSS file; we focus only on the aggregation and analysis implemented through JavaScript.

The div tag is then defined by the index, which is used to describe the layout of the received data. It also contains a form with an input field to enter the URL of the RSS feed, and a submit button to send requests to them. When this button is clicked, a request is sent to receive the RSS feed and the response is sent to an object called aggregator, which is then introduced later in the discussion of the use of AJAX technology for remote RSS feed retrieval.

Iv. cross-Domain AJAX requests

Cross-domain AJAX requests are not possible, but there are 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 an AJAX request and a remote RSS feed to achieve a successful cross-domain request. I think you'll probably be amazed at how easy it is to achieve.

PHP provides a local method called file_get_contents, which can read the entire file contents into a string. If you start the fopen wrapper, the file can be a remote file, which is started by default when you install PHP. If the allow_url_fopen is set to off within the php.ini file, it is disabled. The following code corresponds to the contents of the bridge.php file, and we use index.html to send the request when the form is submitted:

?
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 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 in the index.html file. Once the data is ready, the Ajax engine proxies them to the callback method-our aggregator object.

1

http://www.bkjia.com/PHPjc/446834.html www.bkjia.com true http://www.bkjia.com/PHPjc/446834.html techarticle I. Introduction now, the development of an RSS aggregator is no longer a difficult thing, but the development of a high-quality RSS aggregator is still quite difficult. On the other hand, create a definite ...

  • Related Article

    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.