利用PHP和AJAX創(chuàng)建RSS聚合器(一)
Jun 08, 2016 pm 05:33 PM想象使用一個簡單HTML文件來把一個請求發(fā)送到一個服務(wù)器端腳本,收到一個基于該請求的定制XML文件,然后把它顯示給用戶而幾乎不需要刷新瀏覽器!本文作者將同你一起探討怎樣在普通Web應(yīng)用程序中聯(lián)合PHP和AJAX技術(shù)來創(chuàng)建實時的數(shù)據(jù)傳輸而不需要進行瀏覽器刷新。
盡管本文所使用的是PHP語言,但是請記住任何服務(wù)器端語言都會正常工作。為了理解本文,我假定你基本理解JavaScript和PHP或一類似服務(wù)器端語言。
本文示例使用AJAX來把一請求從一個RSS饋送發(fā)送到一定制的PHP對象。該PHP對象復制一份在本地服務(wù)器上的該饋送并返回這一路徑。該請求對象收到這一路徑,分析它,并且把數(shù)據(jù)以HTML形式顯示給用戶。這聽起來涉及很多步驟,其實它僅由4個小文件組成。之所以使用了4個小文件,是為了平衡它們各自特定的力量而使整個系統(tǒng)的處理極富效率性。
我想,有些讀者可能會問,為什么你要創(chuàng)建在本地服務(wù)器上的饋送的一個副本而不是簡單分析最原始的饋送。原因是,這樣以來可以答應(yīng)繞過XML HTTP Request對象所強加的跨域限制。后面,我還會解釋怎樣創(chuàng)建這個定制的PHP對象;但是首先,讓我們從表單創(chuàng)建開始。
創(chuàng)建發(fā)出請求的表單
你要做的第一事情是,在你的HTML的head標簽之間包括你可能想使用的JavaScript和任何CSS文件。我包括了一個式樣表來實現(xiàn)該聚合器的最后布局并用一個JavaScript文件來發(fā)出請求和進行饋送分析:
下一步,創(chuàng)建一個表單,它針對你所選擇的一個RSS饋送發(fā)出請求。我創(chuàng)建的表單只包括一個輸入字段和一個提交該請求的按鈕。該請求的查詢是一個字符串,它由饋送輸入值和一個將在服務(wù)器端被校驗的口令字組成;作為一個示例,我使用了下面形式:
"password=mypassword
該代碼在每次頁面加載之時發(fā)出一次請求;因此,假如頁面被刷新,現(xiàn)有的在該輸入域中的饋送串將在頁面加載時被請求。下面是一個表單數(shù)據(jù)的示例,連同一些div標簽用來顯示已分析的饋送的特定結(jié)點:
我所創(chuàng)建的這三個div標簽是logo,copy和details,其中每一個都在布局樣式表中有一個與之相關(guān)聯(lián)的樣式。當我們分析饋送時將會用到它們,但是我們首先需要能夠存取我們所請求的饋送。這可以使用我前面所提到的PHP對象來完成。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).
