Home Backend Development PHP Tutorial Create RSS aggregator with PHP and AJAX (1)_PHP tutorial

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

Jul 15, 2016 pm 01:23 PM
ajax php rss but create and device yes server use use polymerization language

尽管本文所使用的是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...

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1280
29
C# Tutorial
1257
24
What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

H5: Key Improvements in HTML5 H5: Key Improvements in HTML5 Apr 28, 2025 am 12:26 AM

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Composer: The Package Manager for PHP Developers Composer: The Package Manager for PHP Developers May 02, 2025 am 12:23 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

How to use type traits in C? How to use type traits in C? Apr 28, 2025 pm 08:18 PM

typetraits are used in C for compile-time type checking and operation, improving code flexibility and type safety. 1) Type judgment is performed through std::is_integral and std::is_floating_point to achieve efficient type checking and output. 2) Use std::is_trivially_copyable to optimize vector copy and select different copy strategies according to the type. 3) Pay attention to compile-time decision-making, type safety, performance optimization and code complexity. Reasonable use of typetraits can greatly improve code quality.

How to configure the character set and collation rules of MySQL How to configure the character set and collation rules of MySQL Apr 29, 2025 pm 04:06 PM

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

How to rename a database in MySQL How to rename a database in MySQL Apr 29, 2025 pm 04:00 PM

Renaming a database in MySQL requires indirect methods. The steps are as follows: 1. Create a new database; 2. Use mysqldump to export the old database; 3. Import the data into the new database; 4. Delete the old database.

See all articles