Using PHP and Ajax to create RSS aggregators

Source: Internet
Author: User
Tags object copy http request php language php and readable string domain
ajax|rss| Create imagine using a simple HTML file to send a request to a server-side script, receive a custom XML file based on the request, and then display it to the user with little need to refresh the browser! The author of this article will explore with you how to combine PHP and Ajax techniques in a common Web application to create real-time data transfers without the need for browser refreshes.

Although this article uses the PHP language, keep in mind that any server-side language will work correctly. To understand this, I assume you have a basic understanding of JavaScript and PHP or a similar server-side language.

This example uses AJAX to send a request from an RSS feed to a certain PHP object. The PHP object copies a copy of the feed on the local server and returns the path. The request object receives this path, analyzes it, and displays the data as HTML to the user. This sounds a lot of steps, but it's only made up of 4 small files. The 4 small files are used to balance their specific strengths and make the entire system highly efficient.

I think 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. The reason for this is that the cross-domain restrictions imposed by the XML HTTP request object can be allowed to be bypassed. Later, I'll explain how to create this custom PHP object, but first, let's start with the form creation.

   Create a form that makes a request
 
The first thing you need to do is to include JavaScript and any CSS files that you might want to use between your HTML head tags. I included a model table to implement the final layout of the aggregator and make a request and feed analysis using a JavaScript file:

<link href= "Css/layout.css" rel= "stylesheet" type= "Text/css"/>
<script src= "Js/request.js" > </script>
Next, create a form that makes a request for an RSS feed of your choice. The form I created consists of an input field and a button that submits the request. The query for the request is a string that consists of a feed input value and a password word that will be validated on the server side; As an example, I used the following form:

"Password=mypassword
The code makes a request every time the page loads, so if the page is refreshed, the existing feed string in the input field will be requested when the page loads. The following is an example of a form data, along with some div tags to display specific nodes for the parsed feed:

<body Password=mypassword '); " >
<form name= "Feedform" method= "post" action= "Javascript:makerequest" (' request.php?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>
<div id= "Copy" > </div>
<div id= "Details" > </div>
</body>


The three div tags I created are logo,copy and details, each of which has a style associated with it in the layout style sheet. They will be used when we analyze the feeds, but we first need to be able to access the feeds we request. This can be done using the PHP object I mentioned earlier.

  Create a custom PHP object

I used PHP to create a small RSS class that creates a copy of the request feed on the local server so that it can be accessed for the XML HTTP request object that we are going to create later. Typically, you cannot request a file across domains, which means that the file you are requesting needs to reside on the local server. This class is a way to resolve a cross-domain problem because it creates a copy of the feed that is requested on the local server and returns the local path to the feed, which is then accessed by the request object.

The only method in this class is a request method, which has only one parameter that points to the URL of the requested RSS feed. It then checks whether a directory resides on the local server by the name of the RSS. If it does not exist, create one and set its permission mode to 0666, which means that the directory is readable and writable. When set to readable, the directory can be accessed at a later time, and when it is set to writable, a copy of the feed can be written to a directory on the local server:

file://If a directory does not exist, create a
$dir = "RSS";
if (!is_dir ($dir))
{
mkdir ($dir, 0666);
}
Attention

On a Windows machine, the mode setting is not required for PHP 4.2.0 and above versions. However, if it exists, it will be ignored; therefore, I reserve it for the project to be migrated to a UNIX or Linux server.

We need a unique file name before copying the feed to the server. I used the MD5 encryption method for this complete URL to make sure that the name of all feeds is unique. With this new filename, it can connect a string that describes the directory that points to the file, which will be used when the copy of the feed is created:

FILE://Create a unique naming
$file =md5 ($rss _url);
$path = "$dir/$file. xml";
We can now create a copy of the file by using a reference to the URL that is defined on the path above and to the original requested feed. Finally, the path is returned to the new file as a response to the request:

file://replication feeds to local server
Copy ($rss _url, "$path");
return $path;
Following is the small, yet powerful RSS class in its entirety:
<?php
Class RSS
{
function get ($rss _url)
{
if ($rss _url!= "")
{
file://If a directory does not exist, create a
$dir = "RSS";
if (!is_dir ($dir))
{
mkdir ($dir, 0666);
}
Create a unique name
$file = MD5 ($rss _url);
$path = "$dir/$file. xml";
file://replication feeds to local server
Copy ($rss _url, "$path");
return $path;
}
}
}
? >

[1] [2] Next page



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.