[PHP] Podcastで使えるRSS作成関数

2020年3月5日

PHP テクノロジー プログラミング

eyecatch ポッドキャスター、ナンチャッテ・エンジニアのユゲタです。 この間スタートした、「ナンチャッテ・ラジオ」ですが、 サイト構築の際に、ポッドキャスト用にRSSを作成する手順があったので、その時に作った関数が、他のサイトでも使えそうだったので、公開しておきたいと思います。 これからpodcastを始めるという人で、自らサイト構築をしている場合にお使いください。

ソース

<?php namespace rss; /** * データ * rss_url : rss-xmlのURL * title : podcastタイトル * url : podcast URL * description : podcast 説明 * docs : メモ * author : 管理者 * banner : バナーURL(大) * email : サービス用メールアドレス * category : main-category * category_sub : sub-category * * program_title : 番組タイトル(毎回) * program_url : 番組詳細URL * program_mp3 : 番組ファイル(mp3) * program_date : 番組公開日 * prigram_description : 番組説明 * */ class podcast{ public static function xml($header=array() , $items=array()){ $path_podcast = __DIR__."/podcast.txt"; $path_header = __DIR__."/podcast_header.txt"; $path_item = __DIR__."/podcast_item.txt"; if(!is_file($path_podcast) || !is_file($path_header) || !is_file($path_item)){return null;} $txt_podcast = file_get_contents($path_podcast); $txt_header = file_get_contents($path_header); $txt_item = file_get_contents($path_item); $header = array_merge($header , self::getBaseInfo()); $txt_header = self::conv($txt_header , $header); $txt_items = self::items($txt_item , $items , $header); $txt_podcast = self::conv($txt_podcast , $header); $txt_podcast = str_replace("{{header}}" , $txt_header , $txt_podcast); $txt_podcast = str_replace("{{items}}" , $txt_items , $txt_podcast); // return $txt_podcast; $xml = new \SimpleXMLElement($txt_podcast); return $xml->asXML(); } public static function getBaseInfo(){ return array( "url" => \mynt::exec('\lib\url\common','getUrl') ); } public static function conv($txt="" , $datas=array()){ if(!$txt){return "";} foreach($datas as $key => $value){ $value = htmlspecialchars($value , ENT_XML1 , "UTF-8"); $txt = str_replace("{{".$key."}}" , $value , $txt); } return $txt; } public static function items($tmp="" , $datas=array() , $header=array()){ if(!$tmp){return "";} $newTxt = ""; foreach($datas as $data){ $current = $tmp; $current = self::conv($current , $data); $current = self::conv($current , $header); $newTxt .= $current; } return $newTxt; } }

使い方

$header = array( "rss_url" => $, // prss_urloscastのrss-url "url" => $url, "title" => $title, "description" => $discription "docs" => $document "author" => $author "banner" => $banner_image, "email" => $mail, "category" => $category_1, "category_sub" => $category_2 ); $datas = array( array( "program_title" => $title "program_url" => $url, "program_mp3" => $mp3, "prigram_description" => $comment, "program_date" => $date ) ); header('Content-Type: text/xml'); echo rss::podcast($header , $datas); 必要な情報を上記のように送ってあげることで、rss(xml)データが表示されます。 ヘッダをつけてechoすると、そのまま表示できるという使い方です。 ファイルに保存して、使うことも可能です。 $datasの箇所は、podcastの番組一覧を配列で登録してください。

最後に

さほど難しい処理ではないのですが、appleのpodcast登録でエラーが出た項目を足していきながら調整して完成したので、 これからpodcastを始めるという方に使ってもらえると有り難いです。 最近仕様が変わったのか分からないですが、他のpodcastのrssを見ながら作った項目なのに、appleの登録サイトで項目が足りないというエラーが出る事態があったので、今後もこの仕様が陳腐化する可能性もありますが、見つける度に更新していこうと思ってます。

人気の投稿

このブログを検索

ごあいさつ

このWebサイトは、独自思考で我が道を行くユゲタの少し尖った思考のTechブログです。 毎日興味がどんどん切り替わるので、テーマはマルチになっています。 もしかしたらアイデアに困っている人の助けになるかもしれません。

ブログ アーカイブ