RSS reader for PHP and AJAX

Source: Internet
Author: User
Tags html form http request php file php and

RSS reader for PHP and AJAX

RSS reader is used to read RSS information

Allows quick RSS viewing of news and updates




Ajax rss reader
In the following example of AJAX, we will show that the content of an RSS reader does not need to be refreshed when loading a webpage from RSS.

In the HTML form
This is an HTML webpage. It contains a simple HTML table and links to a JavaScript:

<Html> 
<Form> Select an RSS-Feed: <select onchange = "showRSS (this. value) "> <option value =" Google "> Google News </option> <option value =" MSNBC "> MSNBC News </option> </select> </form>
<P> <div id = "rssOutput"> <B> RSS Feed will be listed here. </B> </div> </p> </body> 
      

For example, the explanation-h tml form
As you can see, the HTML webpage contains a simple HTML form drop-down box.

The format of the work is as follows:

When an event is triggered, select an option in the drop-down list.
When the event-triggered function requires showRSS () to be executed.
The following form is a <div> so-called "rssOutput ". This is the showRSS () function returned as a placeholder. Reprinted please indicate from http://www.111cn.net/wy/yw.html




JavaScript
JavaScript code is stored in "getrss. js" and related HTML files:

Var xmlHttp
Function showRSS (str) {xmlHttp = GetXmlHttpObject () if (xmlHttp = null) {alert ("Browser does not support HTTP Request") return} var url = "getrss. php "url = url + "? Q = "+ str url = url +" & sid = "+ Math. random () xmlHttp. onreadystatechange = stateChanged xmlHttp. open ("GET", url, true) xmlHttp. send (null)} function stateChanged () {if (xmlHttp. readyState = 4 | xmlHttp. readyState = "complete") {document. getElementById ("rssOutput "). innerHTML = xmlHttp. responseText }}
Function GetXmlHttpObject () {var xmlHttp = null; try {// Firefox, Opera 8.0 +, Safari xmlHttp = new XMLHttpRequest ();} catch (e) {// Internet Explorer try {xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP");} catch (e) {xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ") ;}} return xmlHttp ;}
        

For example
The stateChanged () and GetXmlHttpObject functions the same as the php ajax recommendation chapter.

This showRSS () function

Each selection is to perform this function in the input field as follows:

Defines the URL (file) transmitted to the server
Add the URL of a parameter (Q) and the selected options from the drop-down list
Add a random number to prevent the server from using cached files
Call the GetXmlHttpObject function to create an XMLHTTP object. The stateChanged change is triggered when it describes the functional requirements of the object.
The XMLHTTP object and specific website are opened.
Send an HTTP request to the server



PHP page
The JavaScript code required by the web server is a php file named "getrss. php ":

<? Php // get the q parameter from URL $ q =$ _ GET ["q"];
// Find out which feed was selectedif ($ q = "Google") {$ xml = ("http://news.google.com/news? Ned = us & topic = h & output = rss ");} elseif ($ q =" MSNBC ") {$ xml = (" http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml ");}
$ XmlDoc = new DOMDocument (); $ xmlDoc-> load ($ xml );
// Get elements from "<channel>" $ channel = $ xmlDoc-> getElementsByTagName ('channel')-> item (0 ); $ channel_title = $ channel-> getElementsByTagName ('title')-> item (0)-> childNodes-> item (0)-> nodeValue; $ channel_link = $ channel-> getElementsByTagName ('link')-> item (0)-> childNodes-> item (0)-> nodeValue; $ channel_desc = $ channel-> getElementsByTagName ('Description')-> item (0)-> childNodes-> item (0)-> nodeValue;
// Output elements from "<channel>" echo ("<p> <a href = '". $ channel_link. "'> ". $ channel_title. "</a>"); echo ("<br/>"); echo ($ channel_desc. "</p> ");
// Get and output "<item>" elements $ x = $ xmlDoc-> getElementsByTagName ('ITEM'); for ($ I = 0; $ I <= 2; $ I ++) {$ item_title = $ x-> item ($ I)-> getElementsByTagName ('title')-> item (0) -> childNodes-> item (0)-> nodeValue; $ item_link = $ x-> item ($ I)-> getElementsByTagName ('link')-> item (0) -> childNodes-> item (0)-> nodeValue; $ item_desc = $ x-> item ($ I)-> getElementsByTagName ('Description')-> item (0) -> childNodes-> item (0)-> nodeValue;
Echo ("<p> <a href = '". $ item_link. "'> ". $ item_title. "</a>"); echo ("<br/>"); echo ($ item_desc. "</p>") ;}?> Reprinted please indicate from http://www.111cn.net/wy/yw.html

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.