Create RSS aggregator with PHP and AJAX (1)_PHP tutorial

WBOY
Release: 2016-07-15 13:23:28
Original
726 people have browsed it

尽管本文所使用的是PHP语言,但是请记住任何服务器端语言都会正常工作。为了理解本文,我假定你基本理解JavaScript和PHP或一类似服务器端语言。

本文示例使用AJAX来把一请求从一个RSS馈送发送到一定制的PHP对象。该PHP对象复制一份在本地服务器上的该馈送并返回这一路径。该请求对象收到这一路径,分析它,并且把数据以HTML形式显示给用户。这听起来涉及很多步骤,其实它仅由4个小文件组成。之所以使用了4个小文件,是为了平衡它们各自特定的力量而使整个系统的处理极富效率性。

我想,有些读者可能会问,为什么你要创建在本地服务器上的馈送的一个副本而不是简单分析最原始的馈送。原因是,这样以来可以允许绕过XML HTTP Request对象所强加的跨域限制。后面,我还会解释怎样创建这个定制的PHP对象;但是首先,让我们从表单创建开始。

创建发出请求的表单

你要做的第一事情是,在你的HTML的head标签之间包括你可能想使用的JavaScript和任何CSS文件。我包括了一个式样表来实现该聚合器的最后布局并用一个JavaScript文件来发出请求和进行馈送分析:

<link href="css/layout.css" rel="stylesheet" type="text/css" />

<script src="js/request.js"></script>

下一步,创建一个表单,它针对你所选择的一个RSS馈送发出请求。我创建的表单只包括一个输入字段和一个提交该请求的按钮。该请求的查询是一个字符串,它由馈送输入值和一个将在服务器端被校验的口令字组成;作为一个示例,我使用了下面形式:

"password=mypassword

该代码在每次页面加载之时发出一次请求;因此,如果页面被刷新,现有的在该输入域中的馈送串将在页面加载时被请求。下面是一个表单数据的示例,连同一些div标签用来显示已分析的馈送的特定结点:

<body onload="javascript:makeRequest('request.php?request=' + document.feedForm.feed.value + '"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>

<hr/>

<div id="copy"></div>

<div id="details"></div>

</body>

我所创建的这三个div标签是logo,copy和details,其中每一个都在布局样式表中有一个与之相关联的样式。当我们分析馈送时将会用到它们,但是我们首先需要能够存取我们所请求的馈送。这可以使用我前面所提到的PHP对象来完成。

创建定制的PHP对象

我用PHP创建了一个小型RSS类,它在本地服务器上创建一个请求馈送的副本,这样它可以为我们稍后要创建的XML HTTP Request对象所存取。典型地,你不能跨域请求一个文件,这意味着你要请求的文件需要位于本地服务器上。这个类是一种解决跨域问题的办法,因为它创建该馈送的一个副本,这个副本在本地服务器上被请求并且把本地路径返回到该馈送,然后它由该Request对象来存取。

这个类中唯一的方法是一个请求方法,它仅有一个指向所请求的RSS 馈送的URL的参数。然后,它通过rss的名字来检查是否一目录位于本地服务器上。如果不存在,就创建一个并把其权限模式设置为0666,这意味着该目录可读写。当被设置为可读的时,该目录就可以在以后被存取;而当被设置为可写的时,就可以把该馈送的一个副本写向本地服务器上的目录:

//如果不存在目录就创建一个

$dir = "rss";

if(!is_dir($dir))

{ mkdir($dir, 0666);

}

Note: On a Windows machine, mode setting is not required for PHP 4.2.0 and above. However, if it exists, it will be ignored; therefore, I kept it in case the project was moved to a UNIX or Linux server.

We need a unique filename before copying the feed to this server. I'm using md5 encryption on the full URL to ensure all feed names are unique. With this new filename, it can be concatenated with a string describing the directory pointing to the file; this will be used when creating a copy of the feed:

// Create a unique naming

$file=md5($rss_url);

$path="$dir/$file.xml";

$file =md5($rss_url);

$path="$dir/$file.xml";

We can now create a copy of this file by using the path defined above and a reference to the original requested feed URL. Finally, return the path to the new file in response to the request:

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 != "")

{

//Copy the feed to the local server

//If the directory does not exist, create one
copy($rss_url, "$path");

 $dir = "rss";

 if(!is_dir($dir))

 {

mkdir($dir, 0666);

 }

return $path;

Following is the small, yet powerful RSS class in its entirety:

<?php

class RSS

 $file = md5($rss_url);

 $path = "$dir/$file.xml";

{function get($rss_url)

 {if($rss_url != "")

{

 copy($rss_url, "$path");

 return $path;

}

 }

}

?>

<?

if($password == "mypassword")

{

 require_once('classes/RSS.class.php');

 $rss = new RSS();

 echo $rss->get($request);

}

else

{

 echo "You are an unauthorized user";

}

?>

 $dir = "rss";if(!is_dir($dir))
 {

mkdir($dir, 0666);

// Create a unique name
$file = md5($rss_url); $path = "$dir/$file.xml";
//Copy feed to local server
copy($rss_url, "$path");return $path;}}}?>
In order to access the methods in this PHP class, there needs to be a request file that acts as an interface to the class, which is exactly the file we are requesting. This file first validates a password variable queried from the request, and either returns a message specifying that the requester is not an authorized user, or points to an RSS feed (which is copied to the local server after being processed by the request method). path to respond. In order to respond to this RSS feed, you need to include this RSS object and instantiate it, and you need to activate the request method by using the URL of the requested feed as a parameter:
<?if($password == "mypassword" ){ require_once('classes/RSS.class.php'); $rss = new RSS();echo $rss ->get($request);}else{ echo "You are an unauthorized user"; }?>
1 http://www.bkjia.com/PHPjc/446844.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446844.htmlTechArticleAlthough this article uses PHP language, please remember that any server-side language will work fine. In order to understand this article, I assume you have a basic understanding of JavaScript and PHP or a similar server side...

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template