PHP & JavaScript Room :: 設置サンプル

実行結果

設置サンプル

参考:Google AJAX Feed API

JSON形式の例

各種ニュースサイト(CNET、Google Japan Blog、ITmedia)のRSSフィードを取得して、新着情報3件をHTML出力します。

設置サンプルのソース

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <meta http-equiv="Content-Language" content="ja" />
      <meta http-equiv="Content-Script-Type" content="text/javascript" />
      <meta http-equiv="Content-Style-Type" content="text/css" />
      <meta http-equiv="imagetoolbar" content="no" />
      <title>設置サンプル</title>
      <link rel="stylesheet" type="text/css" href="/content/lib/global.css" />
      <!-- JS -->
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
       <script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAvABrzCnRluKK0Cj97fTMThTNR7aHVSEFb0WbViWwc6F-_w9PjBSogZu_7UgOTbx25ka46X-fnGmyvw"></script>
      <script type="text/javascript">
         google.load("feeds", "1");
         function initialize(){
            init("http://feeds.japan.cnet.com/cnet/rss","feed1","2"); // CNET Japan
            init("http://feeds.feedburner.com/GoogleJapanBlog","feed2","3"); // Google Japan Blog
            init("http://rss.itmedia.co.jp/rss/1.0/topstory.xml","feed3","3"); // ITmedia
         }
         function init(feedurl,obj,feedmax) {
            var feed = new google.feeds.Feed(feedurl);
            feed.setNumEntries(feedmax);
            // フィードをJSON形式で取得
            feed.load(function(result) {
               var container = document.getElementById(obj);
               if (!result.error) {
                  var h2 = document.createElement("h2");
                  h2.appendChild(document.createTextNode(result.feed.title));
                  container.appendChild(h2);
                  
                  var dl = document.createElement("dl");
                  container.appendChild(dl);
                  //alert(result.feed.entries);
                  for (var i = 0; i < result.feed.entries.length; i++) {
                     var entry = result.feed.entries[i];
                     var attributes = ["title", "link","content","contentSnippet","publishedDate","categories"];
                     var dt = document.createElement("dt");
                     var dd = document.createElement("dd");
                     var a=document.createElement("a");
                     for (var j = 0; j < attributes.length; j++) {
                        switch(attributes[j]){
                           case "title":
                              a.appendChild(document.createTextNode(entry[attributes[j]]));
                              break;
                           case "link":
                              a.setAttribute("href",entry[attributes[j]]);
                              break;
                           case "content":
                              //dd.appendChild(document.createTextNode(entry[attributes[j]]));
                              break;
                           case "publishedDate":
                              var date = new Date(entry[attributes[j]]);
                              var dateY=date.getFullYear();
                              var dateM=date.getMonth()+1;
                              var dateD=date.getDate();
                              var str=dateY+"年"+dateM+"月"+dateD+"日";
                              dt.appendChild(document.createTextNode(str));
                              break;
                           case "contentSnippet":
                              //alert(entry[attributes[j]]);
                              break;
                           case "categories":
                              var categories=entry[attributes[j]];
                              break;
                           default:
                              break;
                        }
                        container.appendChild(dt);
                        dd.appendChild(a);
                        container.appendChild(dd);
                     }
                  }
               }else{
                  container.appendChild(document.createTextNode(url));
               }
            });
         }
         google.setOnLoadCallback(initialize);
      </script>
      <!-- CSS -->
      <style type="text/css">
         .feed {
            border:1px solid #ccc;
            background-color:#fff;
            margin:10px auto; padding:10px;
            -webkit-border-radius: 10px;
            -moz-border-radius: 10px;
         }
         .feed h2 {
            margin:0 0 10px 0; padding:0;
         }
         dl {
            margin:0; padding:0;
         }
         dt {
            margin:0; padding:0;
            color:#666;
            font-size:.8em;
         }
         dd {
            margin:0 0 5px 0; padding:0;
         }
      </style>
   <link rel="stylesheet" type="text/css" href="/common/css/example.css"></head>
   <body id='example3' class='example'><div class="ads" style="margin:32px auto;text-align:center;"></div><h1 class='h'><a href='/'>PHP &amp; JavaScript Room</a> :: 設置サンプル</h1>
<h3 class='h'>実行結果</h3>
      <div id="wrap">
         <h1>設置サンプル</h1>
         <p>参考:<a href='http://code.google.com/intl/ja/apis/ajaxfeeds/'>Google AJAX Feed API</a></p>
         <h2>JSON形式の例</h2>
         <p>各種ニュースサイト(CNET、Google Japan Blog、ITmedia)のRSSフィードを取得して、新着情報3件をHTML出力します。</p>
<!-- CODE -->
         <div id="feed1" class="feed"></div>
         <div id="feed2" class="feed"></div>
         <div id="feed3" class="feed"></div>
<!-- / CODE -->
      </div>
   </body>
</html>