- 1. サンプルコードを動かしてみる
- 2. 出力結果
1. サンプルコードを動かしてみる
インストールした中に
/作業ディレクトリ/vendor/openpsa/universalfeedcreator/README.md
ってのがあって、その中にサンプルコードみたいなものが書いてあります。
それをほぼ真似して以下のようなソースを書いてみました。
<?php
require '../vendor/autoload.php';
$rss = new UniversalFeedCreator();
$rss->useCached(); // use cached version if age < 1 hour
$rss->title = "PHP news";
$rss->description = "daily news from the PHP scripting world";
//optional
$rss->descriptionTruncSize = 500;
$rss->descriptionHtmlSyndicated = true;
$rss->link = "http://www.dailyphp.net/news";
$rss->syndicationURL = "http://www.dailyphp.net/" . $_SERVER["PHP_SELF"];
$image = new FeedImage();
$image->title = "dailyphp.net logo";
$image->url = "http://www.dailyphp.net/images/logo.gif";
$image->link = "http://www.dailyphp.net";
$image->description = "Feed provided by dailyphp.net. Click to visit.";
//optional
$image->descriptionTruncSize = 500;
$image->descriptionHtmlSyndicated = true;
$rss->image = $image;
// サンプルではここに MySQL データベースへの接続と SELECT 文があって
// SQL で取得したデータを $rss->addItem($item); で加えている
/*
// get your news items from somewhere, e.g. your database:
mysql_select_db($dbHost, $dbUser, $dbPass);
$res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC");
while ($data = mysql_fetch_object($res)) {
$item = new FeedItem();
$item->title = $data->title;
$item->link = $data->url;
$item->description = $data->short;
//optional
$item->descriptionTruncSize = 500;
$item->descriptionHtmlSyndicated = true;
$item->date = $data->newsdate;
$item->source = "http://www.dailyphp.net";
$item->author = "John Doe";
$rss->addItem($item);
}
*/
$rss->saveFeed('RSS1.0', 'output/rss10.xml', false);
$rss->saveFeed('RSS2.0', 'output/rss20.xml', false);
$rss->saveFeed('ATOM', 'output/atom.xml', false);
?>
30~50行目は、実際には MySQL でデータベースを作成しておいて、そこに情報をいれておくことを想定しているようです。
データベースはないのでコメントアウトしました。
52~54行目は、saveFeed メソッドの第一引数でフォーマットを指定できるようで・・・。
「RSS1.0」で RSS 1.0 の形式、「RSS2.0」で RSS 2.0 の形式、「ATOM」で Atom の形式で出力できるようです。
ただし、最初のうち第三引数を省略していたら、1つ出力した時点で終了していました。
ドキュメントを読んで納得。
ほぼママで書くと
| Parameters |
| string | $format | format the feed should comply to. Valid values are: "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM", "ATOM0.3", "HTML", "JS" |
| string | $filename | optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()). |
| boolean | $displayContents | optional send the content of the file or not. If true, the file will be sent in the body of the response. |
わたしの理解したところでは、PHP は元々ウェブに表示することを想定しているので、コンテンツを表示する属性を true(省略時は true)にするとコンテンツを表示して終了するということなのでしょう。
3ページ一度に表示するなんてウェブブラウザ上ありえませんからね。
2. 出力結果
上記のソースを実行すると下記の結果が得られます。
RSS1.0 形式で出力するとこういう形式
<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.8" -->
<?xml-stylesheet href="http://www.w3.org/2000/08/w3c-synd/style.css" type="text/css"?>
<rdf:RDF
xmlns="http://purl.org/rss/1.0/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel rdf:about="http://www.dailyphp.net/rssmake.php">
<title>PHP news</title>
<description>daily news from the PHP scripting world</description>
<link>http://www.dailyphp.net/news</link>
<image rdf:resource="http://www.dailyphp.net/images/logo.gif" />
<dc:date>2018-10-19T16:44:47+0900</dc:date>
<items>
<rdf:Seq>
</rdf:Seq>
</items>
</channel>
<image rdf:about="http://www.dailyphp.net/images/logo.gif">
<title>dailyphp.net logo</title>
<link>http://www.dailyphp.net</link>
<url>http://www.dailyphp.net/images/logo.gif</url>
</image>
</rdf:RDF>
RSS2.0 形式で出力するとこういう形式
<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.8" -->
<rss version="2.0">
<channel xmlns:g="http://base.google.com/ns/1.0">
<title>PHP news</title>
<description><![CDATA[daily news from the PHP scripting world]]></description>
<link>http://www.dailyphp.net/news</link>
<lastBuildDate>Fri, 19 Oct 2018 16:44:47 +0000</lastBuildDate>
<generator>FeedCreator 1.8</generator>
<image>
<url>http://www.dailyphp.net/images/logo.gif</url>
<title>dailyphp.net logo</title>
<link>http://www.dailyphp.net</link>
<description>Feed provided by dailyphp.net. Click to visit.</description>
</image>
</channel>
</rss>
Atom 形式で出力するとこういう形式
<?xml version="1.0" encoding="utf-8"?>
<!-- generator="FeedCreator 1.8" -->
<feed xmlns="http://www.w3.org/2005/Atom">
<title>PHP news</title>
<subtitle>daily news from the PHP scripting world</subtitle>
<link rel="alternate" type="text/html" href="http://www.dailyphp.net/news"/>
<id>http://www.dailyphp.net/news</id>
<updated>2018-10-19T16:44:47+0900</updated>
<generator>FeedCreator 1.8 (info@mypapit.net)</generator>
<link rel="self" type="application/atom+xml" href="http://www.dailyphp.net/rssmake.php" />
</feed>
この Atom 形式のものを RSS リーダで見てみると

う~ん、なるほど、これを手直ししていけば、自分のサイトっぽくできるのですね。
|