PHP and Ajax RSS reader

Source: Internet
Author: User
ArticleDirectory
    • Example-HTML form
    • Example:
    • Example:

RSS reader is used to read RSS feeds.

RSS allows you to quickly browse news and updates.

Ajax RSS reader

In the following Ajax example, we will demonstrate an RSS reader that loads content from RSS into the webpage without refreshing it.

Select an RSS news subscription from the list box below

 

RSS feed is listed here.

 

This example includes three elements:

    • Simple HTML form
    • Javascript
    • PHP page
HTML form

This is an HTML page. It contains a simple HTML form and a link to execute a Javascript file:

<HTML>  
Example-HTML form

As you can see, the preceding HTML page contains a simple HTML form with a drop-down list box.

The form works like this:

    1. When you select the option in the drop-down box, an event is triggered.
    2. When an event is triggered, run the showrss () function.

Below the form is a <div> named "rssoutput. It is used as a placeholder for the data returned by the showrss () function.

Javascript

JavascriptCodeStored in "getrss. js", which is connected to HTML documents:

VaR xmlhttpfunction 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 ;}
Example:

The statechanged () and getxmlhttpobject functions are the same as the examples in the section PHP and Ajax requests.

Showrss () function

Each time you select a function in the drop-down list, the function is executed:

    1. Define the URL (File Name) sent to the server)
    2. Add the parameter (q) to the URL. The parameter content is the option in the drop-down box.
    3. Add a random number to prevent files from being cached by the server.
    4. Call the getxmlhttpobject function to create an XMLHTTP object and notify the object to execute the statechanged function when a change is triggered.
    5. Use the given URL to open XMLHTTP
    6. Initiate an HTTP request to the server
PHP page

The server page that calls JavaScript code 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>") ;}?>
Example:

When an option is sent from JavaScript, the following occurs:

    1. PHP finds out which RSS feed is selected
    2. Create an xml dom object for the selected RSS Feed
    3. Find and output the elements from the RSS Feed
    4. Traverse the elements in the first three RSS items and output them in parallel

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.