クロスドメインでのXML取得を、phpを使って実装してみます。
同一ドメインの場合
まずは、同一ドメインの場合のXML取得のサンプルです。
HTML
1 | < div id = "sample" ></ div > |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | $( function () { $.ajax({ dataType: 'xml' , success: function (data) { // 取得件数 var getCount = 5; if ($( 'item' , data).length < getCount) { getCount = data.length; }; var insert = '' ; insert += '<h1>' ; // リンク先の挿入 insert += '<a href="' + $( 'channel' , data).children( 'link' ).text() + '">' ; // タイトルの挿入 insert += $( 'channel' , data).children( 'title' ).text(); insert += '</a>' ; insert += '</h1>' ; insert += '<ul>' ; for ( var i = 0; i < getCount; i++) { var thisItem = $( 'channel' , data).children( 'item' ).eq(i); insert += '<li>' ; // 日時の挿入 insert += '<p class="pubDate">' ; insert += thisItem.children( 'pubDate' ).text(); insert += '</p>' ; // タイトルの挿入 insert += '<p class="title">' ; insert += thisItem.children( 'title' ).text(); insert += '</p>' ; // リンク先の挿入 insert += '<a href="' + thisItem.children( 'link' ).text() + '">詳細はコチラ</a>' ; insert += '</li>' ; }; insert += '</ul>' ; $( '#sample' ).append(insert); } }); }); |
この読み込むXMLを別ドメインのものに変更しても、このままではうまく表示されません。
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | $( function () { $.ajax({ dataType: 'xml' , success: function (data) { // 取得件数 var getCount = 5; if ($( 'item' , data).length < getCount) { getCount = data.length; }; var insert = '' ; insert += '<h1>' ; // リンク先の挿入 insert += '<a href="' + $( 'channel' , data).children( 'link' ).text() + '">' ; // タイトルの挿入 insert += $( 'channel' , data).children( 'title' ).text(); insert += '</a>' ; insert += '</h1>' ; insert += '<ul>' ; for ( var i = 0; i < getCount; i++) { var thisItem = $( 'channel' , data).children( 'item' ).eq(i); insert += '<li>' ; // 日時の挿入 insert += '<p class="pubDate">' ; insert += thisItem.children( 'pubDate' ).text(); insert += '</p>' ; // タイトルの挿入 insert += '<p class="title">' ; insert += thisItem.children( 'title' ).text(); insert += '</p>' ; // リンク先の挿入 insert += '<a href="' + thisItem.children( 'link' ).text() + '">詳細はコチラ</a>' ; insert += '</li>' ; }; insert += '</ul>' ; $( '#sample' ).append(insert); } }); }); |
別ドメインの場合
方法はいくつかあるみたいですが、今回はPHPで一回取得してから、それを読み込むようにします。
PHP
1 2 3 4 5 | <?php $xml = file_get_contents ( $url ); header( "Content-type: application/xml; charset=UTF-8" ); print $xml ; |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | $( function () { $.ajax({ url: 'get.php' , dataType: 'xml' , success: function (data) { // 取得件数 var getCount = 5; if ($( 'item' , data).length < getCount) { getCount = data.length; }; var insert = '' ; insert += '<h1>' ; // リンク先の挿入 insert += '<a href="' + $( 'channel' , data).children( 'link' ).text() + '">' ; // タイトルの挿入 insert += $( 'channel' , data).children( 'title' ).text(); insert += '</a>' ; insert += '</h1>' ; insert += '<ul>' ; for ( var i = 0; i < getCount; i++) { var thisItem = $( 'channel' , data).children( 'item' ).eq(i); insert += '<li>' ; // 日時の挿入 insert += '<p class="pubDate">' ; insert += thisItem.children( 'pubDate' ).text(); insert += '</p>' ; // タイトルの挿入 insert += '<p class="title">' ; insert += thisItem.children( 'title' ).text(); insert += '</p>' ; // リンク先の挿入 insert += '<a href="' + thisItem.children( 'link' ).text() + '">詳細はコチラ</a>' ; insert += '</li>' ; }; insert += '</ul>' ; $( '#sample' ).append(insert); } }); }); |
【参考サイト】
こちらの記事を参考にブログの表示が出来ました!ありがとうございます。
ただ、上記記事のように、日付を2017.01.01みたいに表示させたいと思うのですが表示が上手くできません。
また、ブログの内容を150字程度表示させる事は可能でしょうか?
表示させたいものは
日付(2017.01.01みたいな)+ブログ記事のタイトル+ブログ記事の内容(159字程度)です。
ひひさん
コメントありがとうございます。
使用するXMLにもよるかと思いますが、
日付と本文ともに出力前に整形すれば可能です。
以下のサンプルページをご確認頂ければと思います。
https://cly7796.net/blog/sample/ajax-get-xml/index6.html
何度もすみません。
HTMLでの表示はできました。
・記事内の最初の画像の表示
・記事内に画像がなかった場合の代わりの画像表示
・タイトルにリンクをつける
以上のようなことはできますでしょうか?
つっちーさん
コメントありがとうございます。
→こちらはXMLにサムネイルの項目があれば表示できますが、記事の本文中にある画像を表示する、ということでしたら難しいかもしれません。
→こちらは以下にサンプルをおいています。
https://cly7796.net/blog/sample/ajax-get-xml/index5.html
下記ファイルの31~36行目でタイトルにリンクをつけています。
https://cly7796.net/blog/sample/ajax-get-xml/sample5.js
HTMLで作成したホームページにこちらを使用することはできるのでしょうか?
日付を2017.01.01みたいに表示させることは出来るのでしょうか?
yamaさん
コメントありがとうございます。
JavaScriptで出力する前に日付を整形すれば可能です。
以下にサンプルページを置いていますので、コードをご確認頂ければと思います。
https://cly7796.net/blog/sample/ajax-get-xml/index4.html
日付の整形については過去に記事を書いていますので、よければ合わせてご確認ください。
https://cly7796.net/blog/javascript/date-format/
ありがとうございます。
大変参考になりました。