Home > php教程 > php手册 > body text

php ajax异步读取rss文档数据,

WBOY
Release: 2016-06-13 08:42:39
Original
1033 people have browsed it

php ajax异步读取rss文档数据,

RSS(Really Simple Syndication)是一种描述和同步网站内容的格式,是使用最广泛的XML应用。RSS搭建了信息迅速传播的一个技术平台,使得每个人都成为潜在的信息提供者。

本文实例为大家分享了php ajax异步读取rss文档数据的相关代码,供大家参考,具体内容如下

代码如下:

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>

Copy after login

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;>
Copy after login

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

您可能感兴趣的文章:

  • 用PHP读取RSS feed的代码
  • PHP 正则表达式分析RSS
  • PHP调用Twitter的RSS的实现代码
  • php中通过Ajax如何实现异步文件上传的代码实例
  • php XMLWriter类的简单示例代码(RSS输出)
  • 利用ThinkPHP内置的ThinkAjax实现异步传输技术的实现方法
  • PHP+Ajax异步通讯实现用户名邮箱验证是否已注册( 2种方法实现)
  • Ajax异步传输与PHP实现交互示例
  • PHP curl 抓取AJAX异步内容示例
  • php生成RSS订阅的方法
Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template