PHPでRSSリーダー

Fritzのホームページでニュースを表示するために
Yahooのニュース用RSSを解析してページを展開する
PHPのコードを書いたので公開します。


ソース
  
ソースは以下のようになっています。
@のブロックでRSSのURLを指定します。
Aでは、そのRSSを読み込みます。
Bのブロックで読み込んだRSSを解析します。
Cのブロックで解析したRSSデータをHTMLとして吐き出します。

<?php

	/* ===================================================================== *
		RSS Reader	Version 1.00
		Program Written By Fritz, All rights reserved.
	 * ===================================================================== */

	//
	// 	@ set url to read from
	//

	$url = "http://dailynews.yahoo.co.jp/fc/rss.xml";

	//
	//	A read RSS data from url
	//

	$data = file($url);

	//
	// 	B parse RSS Data
	//

	$flag = 0 ;
	$output = Array();

	foreach( $data as $ikey => $idata )
	{
		//
		//	keyword scan
		//

		if( substr($idata,0,6) == "<item>")
		{
			$flag = 1 ;
			$ttl = "" ;
			$lnk = "" ;
		}
		else if ( substr($idata,0,7) == "</item>" )
		{
			$flag = 0 ;
			if ( strlen($ttl) > 0 && strlen($lnk) > 0 )
			{
				$output[] = "<a style='cursor:hand' onclick='jump_page(" . '"' . $lnk . '"' . ");'><font size=-1 color=blue><U><img width=10 height=7 src='http://www.spanky-world.com/images/cliparts/arrow_s.jpg'>" . $ttl . "</U></a><BR>\n" ;
			}
		}
		else if ( substr($idata,0,9) == "<pubDate>" )
		{
			$pub = str_replace("<pubDate>","", $idata);
			$pub = str_replace("</pubDate>","", $pub);
			$pub = str_replace("\n","", $pub);
			$pub = substr($pub,0,25);
		}

		//
		//	data read
		//

		if ( $flag == 1 )
		{
			if( substr($idata,0,7) == "<title>" )
			{
				$ttl = str_replace( "<title>", "", $idata ) ;
				$ttl = str_replace( "</title>", "", $ttl ) ;
				$ttl = str_replace( "\n", "", $ttl ) ;
			}
			if ( substr($idata,0,6) == "<link>" ) 
			{
				$lnk = str_replace( "<link>", "", $idata ) ;
				$lnk = str_replace( "</link>", "", $lnk ) ;
				$lnk = str_replace( "\n", "", $lnk ) ;
			}
		}
	}

	//
	// 	C Display everything
	//

	print("<html>\n");
	print("<header>\n");
	print('<META http-equiv="Content-Type" content="text/html; charset=Utf-8">');
	print('<script LANGUAGE="JavaScript" Type="text/javascript">');
	print("\n");
	print("function jump_page(url)\n");
	print("{\n");
	print("	myD = new Date();\n");
	print('	myX = "_pop" + myD.getYear() + myD.getMonth() + myD.getDate() + myD.getDay() + myD.getHours() + myD.getMinutes() + myD.getSeconds();');
	print("\n");
	print("	category_root=window.open(url,myX,'');\n");
	print("}\n");
	print("</script>\n");
	print("</header>\n");
	print("<body bgcolor=#e0e0f0>\n");
	print("<center>\n");
	print("<table>");
	print("<tr><td height=2></td></tr>");
	print("<tr><td><center>");
	print("<font size=-1>Fritz ニュース ヘッドライン</font><BR>\n");
	print("<font size=-2 color=green>frm Yahoo! News</font><BR>");
	print("<font size=-2 color=green>" . $pub . "</font><BR>\n");
	print("</td></tr>");
	print("<tr><td height=5></td></tr>");
	print("</table>");
	print("<table width=235 border=0 cellspacing=0 cellpadding=0>\n");
	print("<tr><td>\n");

	foreach( $output as $odata )
	{
		print( $odata ) ;
	}

	print("</td></tr>\n");
	print("</table>");
	print("</body>\n");
	print("</html>\n");

	exit;

?>


上に戻る




戻る