外部ブログの情報をRSSで取得し、ブラウザに表示する
RSS配信用のxmlファイルを設置しているブログの情報は自動取得して表示することができます。
自動取得できるかどうかの見分け方
このオレンジのアイコンが出ていれば可能
(これはChromeでRSSワンタッチ登録拡張機能をインストールしている場合に表示されます)
先生のブログ「講師記録」の記事見出しを自動取得
index.php
<!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> <h3>rssを取得</h3> <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",strtotime($dc->date)); //$desc = $item->description; echo" <li><a href=\"$link\" title=\"$title\" target=\"_blank\">$title</a><span>($date)</span></li>\n"; } ?> </ul> </body> </html>
Yahooニューストピックスのトップ8を取得・表示します
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Yahoo</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); $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>
iTunes Top 10 ソングのRSS情報を取得して表示します
<!DOCTYPE html> <html> <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</a>($date)</li>\n"; } echo'</ol>'; ?> </body> </html>