Home >Backend Development >PHP Tutorial >php ajax asynchronously reads rss document data, _PHP tutorial

php ajax asynchronously reads rss document data, _PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:55:15845browse

php ajax reads RSS document data asynchronously,

RSS (Really Simple Syndication) is a format for describing and synchronizing website content, and is the most widely used XML application. RSS has built a technical platform for rapid information dissemination, making everyone a potential information provider.

The example in this article shares with you the relevant code for php ajax to read RSS document data asynchronously for your reference. The specific content is as follows

The code is as follows:

index.html

<html>

 <head>
  <script>
   function showRSS(str) {
   if (str.length==0) { 
    document.getElementById("output").innerHTML="";
    return;
   }   
   if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest();
   }
   else 
   {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
   xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
     document.getElementById("output").innerHTML=xmlhttp.responseText;
    }
   }   
   xmlhttp.open("GET","rss.php&#63;q="+str,true);
   xmlhttp.send();
   }
  </script>
 </head>
 <body>
  <p>Please Select an option to get RSS:</p>  
  <form>
   <select onchange="showRSS(this.value)">
   <option value="">Select an RSS-feed:</option>
   <option value="cnn">CNN</option>
   <option value="bbc">BBC News</option>
   <option value="pc">PC World</option>
   </select>
  </form>
  <br>  
  <div id="output">RSS-feeds</div>  
 </body>
</html>

rss.php

<&#63;php
 $q=$_GET["q"]; 
 if($q=="cnn") {
  $xml=("http://rss.cnn.com/rss/cnn_topstories.rss");
 } 
 elseif($q=="bbc") {
  $xml=("http://newsrss.bbc.co.uk/rss/newsonline_world_edition/americas/rss.xml");
 } 
 elseif($q="pcw"){
  $xml=("http://www.pcworld.com/index.rss");
 }
 $xmlDoc = new DOMDocument();
 $xmlDoc->load($xml); 
 $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;
 echo("<p><a href='" . $channel_link . "'>" . $channel_title . "</a>");
 echo("<br>");
 echo($channel_desc . "</p>");
 $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>");
 }
&#63;>

The above is the entire content of this article. I hope it will be helpful to everyone in learning PHP programming.

Articles you may be interested in:

  • Code to read RSS feed with PHP
  • PHP regular expression analysis RSS
  • PHP calls Twitter’s RSS Implementation code
  • Code example of how to implement asynchronous file upload through Ajax in php
  • Simple example code of php XMLWriter class (RSS output)
  • Using ThinkPHP’s built-in ThinkAjax implementation Implementation method of asynchronous transmission technology
  • PHP Ajax asynchronous communication to implement user name and email verification whether registered (2 methods to implement)
  • Ajax asynchronous transmission and PHP interaction example
  • PHP curl grabs AJAX asynchronous content example
  • php method to generate RSS subscription

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1117074.htmlTechArticlephp ajax reads RSS document data asynchronously. RSS (Really Simple Syndication) is a way to describe and synchronize website content. format, is the most widely used XML application. RSS builds a platform for rapid information dissemination...
Statement:
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