用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - php代码库

RSS阅读器实例 PHP - AJAX

2014-12-09 作者: php源代码大全举报

[php]代码库

 <html>
 <head>
 <script>
 function showRSS(str)
 {
 if (str.length==0)
 { 
 document.getElementById("rssOutput").innerHTML="";
 return;
 }
 if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 }
 else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 xmlhttp.onreadystatechange=function()
 {
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
 {
 document.getElementById("rssOutput").innerHTML=xmlhttp.responseText;
 }
 }
 xmlhttp.open("GET","getrss.php?q="+str,true);
 xmlhttp.send();
 }
 </script>
 </head>
 <body>

 <form>
 <select onchange="showRSS(this.value)">
 <option value="">Select an RSS-feed:</option>
 <option value="Google">Google News</option>
 <option value="MSNBC">MSNBC News</option>
 </select>
 </form>
 <br>
 <div id="rssOutput">RSS-feed will be listed here...</div>
 </body>
 </html> 


















 <?php
 //get the q parameter from URL
 $q=$_GET["q"];

 //find out which feed was selected
 if($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>");
 }
 ?> 


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...