RSSを取得
外部ブログのRSSを取得し表示
- PHP5で標準実装されたsimplexmlモジュールを利用する
simplexml
- simplexmlとは、XML文書を手軽に処理することができるモジュールです
- PHP での SimpleXML 処理
XMLファイルの読み込み
- simplexml_load_file
- 引数には、読み込むXMLファイルを記述します
$rss = simplexml_load_file("rss.xml");
- サーバー設定でallow_url_fopenが有効な場合は、ファイル名のところをURL(http://〜の形)で表記することができ、他サイトのRSS等を処理することもできます
- ただし他サイトのRSSを表示する場合はサニタイジング等のセキュリティ処理にも注意してください
- サニタイジングとは、クロスサイトスクリプティング(XSS)の攻撃から、テキストデータ上の「&」や「>」など特殊文字を一般的な文字列に変換する処理のことにより意味をもたない文字列として扱う処理のことです
$rss = simplexml_load_file("http://d.hatena.ne.jp/css_design/rss");
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>外部RSSを取得・表示</title> <style> li { font-size: 0.875em; line-height: 1.4; margin-bottom: 4px; } </style> </head> <body> <ul> <?php $rss = simplexml_load_file("http://d.hatena.ne.jp/css_design/rss"); $i = 1; foreach ($rss -> item as $item) { if(++$i>5) break; $dc = $item->children('http://purl.org/dc/elements/1.1/'); $link = $item->link; $title = $item->title; $date = date("Y/m/d"); //$desc =$item->description; echo "<li><a href=\"$link\" title=\"$title\" target=\"_blank\">$title</a><span>($date)</span></li>\n"; } ?> </ul> </body> </html>
Yahoo! トピックスの RSS を表示
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>Yahoo! トピックスの RSS を表示</title> </head> <body> <h3>RSSを表示</h3> <?php $url = 'http://rss.dailynews.yahoo.co.jp/fc/rss.xml'; $rss = file_get_contents($url); $xml = simplexml_load_string($rss); $dc = $item->children('http://purl.org/dc/elements/1.1/'); $channel = $xml->channel; $feed_title = $channel->title; echo '<h3>'.$feed_title.'</h3>'; echo '<ol>'; foreach ($channel->item as $item) { $link = $item->link; $title = $item->title; $date = date("Y年m月d日"); $desc = $item->description; echo "<li><a href=\"$link\" title=\"$title\" target=\"_blank\">$title</a><span>($date)</span></li>\n"; } echo '</ol>'; ?> </body> </html>
アップル - ホットニュースの RSS を表示
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>アップル - ホットニュース</title> </head> <body> <h3>RSS から取得して表示</h3> <?php $url = 'http://www.apple.com/jp/main/rss/hotnews/hotnews.rss'; $rss = file_get_contents($url); $xml = simplexml_load_string($rss); $channel = $xml->channel; $feed_title = $channel->title; echo '<h3>'.$feed_title.'</h3>'; echo '<ol>'; foreach ($channel->item as $item) { $link = $item->link; $title = $item->title; $date = date("Y年m月d日"); $desc = $item->description; echo "<li><a href=\"$link\"title=\"$title\">$title</a>($date)</li>\n"; } echo '</ol>'; ?> </body> </html>
アップル - 新規追加トップ10の RSS を表示
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>アップル - 新規追加トップ10</title> </head> <body> <h3>RSS から取得して表示</h3> <?php $url = 'http://ax.itunes.apple.com/WebObjects/MZStore.woa/wpa/MRSS/justadded/sf=143462/limit=10/rss.xml'; $rss = file_get_contents($url); $xml = simplexml_load_string($rss); $channel = $xml->channel; $feed_title = $channel->title; echo '<h3>'.$feed_title.'</h3>'; echo '<ol>'; foreach ($channel->item as $item) { $link = $item->link; $title = $item->title; $date = date("Y年m月d日"); $desc = $item->description; echo "<li><a href=\"$link\"title=\"$title\">$title</a>($date)</li>\n"; } echo '</ol>'; ?> </body> </html>
アップル - トップ10ソングの RSS を表示
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>アップル - トップ10ソング</title> </head> <body> <h3>RSS から取得して表示</h3> <?php $url = 'http://ax.itunes.apple.com/WebObjects/MZStore.woa/wpa/MRSS/topsongs/sf=143462/limit=10/rss.xml'; $rss = file_get_contents($url); $xml = simplexml_load_string($rss); $channel = $xml->channel; $feed_title = $channel->title; echo '<h3>'.$feed_title.'</h3>'; echo '<ol>'; foreach ($channel->item as $item) { $link = $item->link; $title = $item->title; $date = date("Y年m月d日"); $desc = $item->description; echo "<li><a href=\"$link\"title=\"$title\">$title</a>($date)</li>\n"; } echo '</ol>'; ?> </body> </html>