<?php $num = 5;//RSS取得件数 $rssUrl=array( 'http://x6xo.hatenablog.com/rss',//サイトURL ); //magpierss require_once('./magpierss-master/rss_fetch.inc'); define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');//encode define('MAGPIE_CACHE_AGE','30');//cache foreach($rssUrl as $no => $rss_url){ if($rss_url != ''){ //urlからRSSを取得 $rss = @fetch_rss($rss_url); if($rss != NULL){ for ($i=0; $i<count($rss->items); $i++){ $rss->items[$i]["site_title"] = $rss->channel["title"]; $rss->items[$i]["site_link"] = $rss->channel["link"]; } // itemsを格納 $rssItemsArray[] = $rss->items; } } } $concatArray = array(); if (is_array($rssItemsArray)) { for($i=0;$i<count($rssItemsArray);$i++){ $concatArray = array_merge($concatArray,$rssItemsArray[$i]);//配列を統合する } foreach ($concatArray as $no => $values) { //RSSの種類によって日付を取得 if($values['published']){$date = $values['published'];} elseif($values['created']){$date = $values['created'];} elseif($values['pubdate']){$date = $values['pubdate'];} elseif($values['dc']['date']){$date = $values['dc']['date'];} $date=date("Y-m-d H:i:s",strtotime($date)); //Filter $nowtime = date("Y-m-d H:i:s",strtotime( "now" ));//現在時刻の取得 if($date > $nowtime){//未来記事の排除 }elseif(preg_match("/AD/", $values["title"])){//広告記事の排除 }elseif(preg_match("/PR/", $values["title"])){ }else{ //値の定義 $title=$values["title"]; $link=$values["link"]; $site_title=$values["site_title"]; $site_link=$values["site_link"]; //記事ごとに必要な項目を抽出 $rssArray[]=array($date, $title, $link, $site_title, $site_link); }// }// //ソート function cmp($a, $b) { if ($a[0] == $b[0]) return 0; return ($a[0] > $b[0]) ? -1 : 1; } if($rssArray) { usort($rssArray, 'cmp'); } if(count($rssArray) > $num){$count=$num;} else{$count=count($rssArray);} for ($i=0; $i<$count; $i++) { $date=date("Y-m-d H:i:s",strtotime($rssArray[$i][0])); $title=$rssArray[$i][1]; $link=$rssArray[$i][2]; $site_title=$rssArray[$i][3]; $site_link=$rssArray[$i][4]; $datelink = "<div>$date"; $titlelink = "<a href='$link'>$title</a>"; $site_titlelink = "<a href='$site_link'>[$site_title]</a></div>"; echo "$datelink$titlelink$site_titlelink</div>";//(確認用) } } ?>
mysql
create table rss( id int not null auto_increment primary key, title varchar(255), link varchar(255), site_title varchar(64), site_link varchar(64), date datetime );