-
[PHP] 54. AJAX RSS Reader - PHP 강좌, PHP5Web/PHP 2015. 6. 13. 14:15
PHP Example - AJAX RSS Reader
RSS 리더는 RSS 피드를 읽을 때 사용됩니다.
1. AJAX RSS Reader
아래 예제는 웹페이지 리로드 없이 RSS-피드를 불러오는 예제입니다.
2. Example Explained - The HTML Page
1234567891011121314151617181920212223242526272829303132333435363738<html><head><script>function showRSS(str) {if (str.length==0) {document.getElementById("rssOutput").innerHTML="";return;}if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safarixmlhttp=new XMLHttpRequest();} else { // code for IE6, IE5xmlhttp=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=""> My Blog Post </option><option value="Google">Google News</option><option value="NBC">NBC News</option></select></form><br><div id="rssOutput">RSS-feed will be listed here...</div></body></html>cs 3. The PHP File
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647<?php//get the q parameter from URL$q=$_GET["q"];//find out which feed was selectedif($q=="Blog") {$xml=("http://zhfldi4.blog.me/rss");}else if($q=="Google") {} elseif($q=="NBC") {}$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>");}?>cs * 이 강좌는 'w3schools'를 참조하여 작성하였습니다.
'Web > PHP' 카테고리의 다른 글
[PHP] 55. AJAX Poll - PHP 강좌, PHP5 (0) 2015.06.13 [PHP] 53. AJAX 실시간 검색 ( AJAX Live Search) - PHP 강좌, PHP5 (0) 2015.06.13 [PHP] 52. AJAX 와 XML ( AJAX and XML) - PHP 강좌, PHP5 (0) 2015.06.13 [PHP] 51. AJAX 와 MySQL ( AJAX and MySQL) - PHP 강좌, PHP5 (0) 2015.06.13 [PHP] 50. AJAX 와 PHP(AJAX and PHP) - PHP 강좌, PHP5 (0) 2015.06.13 [PHP] 49. AJAX 소개( AJAX Introduction) - PHP 강좌, PHP5 (0) 2015.06.13