RSS関連のメモ | SEO・アフィリ・PHPに関するブログ

RSS関連のメモ

<content:encoded>の中身を取り出す
普段RSSをPHPで抜き出すときは私は simplexml_load_file を仕様してますが
その場合 
(string)($rssdata->channel->item[$cnt]->content:encoded)
と記述するとPHPエラーになるので、<content:encoded>を抜き出すときは
一度、ファイルを文字列で読み込んで置換して
simplexml_load_stringで読み込みます。

//RSSのURL
$rssurl = 'http://www.acmilan.com/jp/feed';
//このRSSは原稿が「content:encoded」にはいっているので、一旦読み込んでから置換して解析する
$file = str_replace("<content:", '<content_', file_get_contents($rssurl));
$file = str_replace("</content:", '</content_', $file);
$rssdata = simplexml_load_string($file);

そしてこれで値を取得できる
//<content:encoded>の中身を取得
$getDat = (string)($rssdata->channel->item[$cnt]->content_encoded);


@attributesの中身を取り出す
//リンク
$getDat = (string)($rssdata->entry[$cnt]->link->attributes()->href);
これでOK