【PHP】AKB48の最新5件RSS取得ソースコード

RSSを取得するサンプルプログラムです。

アメブロのAKB48のブログから取得しました。
AKB48からRSSを取得したかというとただ単にタイトルの切り出しとかやりたかっただけです。
RSSの日付を日本時間に変換したり、UTF-8での文字列の切り出しを行っています。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>AKB48のRSS取得してみる</title>
</head>
<body>
<?php
//アメブロ取得関数
function rss(){
  //AKB48 RSS
  $url = "http://feedblog.ameba.jp/rss/ameblo/akihabara48/rss20.xml";
  //表示件数
  $count = 5;
  //タイトル切り出し文字数
  $str = 27;
 
  //XMLパース
  $xml = simplexml_load_file($url) or die("XMLパースエラー");
 
  echo "<ul>";
  for( $i = 0; $i < $count; $i++ ){
    if( mb_strlen( $xml->channel->item[$i]->title, "UTF-8" ) >= $str ){
      $title = mb_substr(mb_strtoupper($xml->channel->item[$i]->title, "utf-8"),0,$str -2,'utf-8' );
      $title .= "…";
    }else{
      $title = $xml->channel->item[$i]->title;
    }
    echo "<li class="news_02">".date( 'Y年m月d日', strtotime( $xml->channel->item[$i]->pubDate ) )." <a href ="".$xml->channel->item[$i]->link."">".$title."</a>";
  }
  echo "</ul>";
}
?>
<?php
  rss();
?>
</body>
</html>

サンプルを実行する

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x