我在数据库中有一组文章我想使用xml格式将其内容添加到位于我的项目rss.xml中的文件中。 这是来自https://developers.facebook.com/docs/instant-articles/publishing/setup-rss-feed的xml文件。
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>News Publisher</title>
<link>http://www.example.com/</link>
<description>
Read our awesome news, every day.
</description>
<language>en-us</language>
<lastBuildDate>2014-12-11T04:44:16Z</lastBuildDate>
<item>
<title>This is an Instant Article</title>
<link>http://example.com/article.html</link>
<guid>2fd4e1c67a2d28fced849ee1bb76e7391b93eb12</guid>
<pubDate>2014-12-11T04:44:16Z</pubDate>
<author>Mr. Author</author>
<description>This is my first Instant Article. How awesome is this?</description>
<content:encoded>
<!doctype html>
<html lang="en" prefix="op: http://media.facebook.com/op#">
<head>
<meta charset="utf-8">
<link rel="canonical" href="http://example.com/article.html">
<meta property="op:markup_version" content="v1.0">
</head>
<body>
<article>
<header>
<!— Article header goes here -->
</header>
<!— Article body goes here -->
<footer>
<!— Article footer goes here -->
</footer>
</article>
</body>
</html>
</content:encoded>
</item>
</channel>
</rss>
这是我在我的PHP中走了多远:
$crud = new ArticleController();
$file = 'rss.xml'; //open the file
$xml = simplexml_load_file($file);
$channel = $xml->channel; //get channel to add item to
$list=$crud->getAll(); //Returns all articles in database
$item = $channel->addChild('item');
$item->addChild('title', 'a gallery');
$item->addChild('pubDate', '12/12/2017');
$item->addChild('description', 'something');
$content = $item->addChild('content:encoded');
$html = $content->addChild('html');
$xml->asXML($file); //write to file
由于我的代码已经返回了很多警告和错误,所以我不会走得太远:
Warning: simplexml_load_file(): rss.xml:25: parser error : Opening and ending tag mismatch: meta line 24 and head in file.php on line 153
Fatal error: Call to a member function children() on a non-object in /var/www/html/pfe2017/controller/ArticleController.php on line 156
有人可以通过提供示例帮助解释如何实现预期结果吗?
答案 0 :(得分:2)
根据RSS Feeds for Instant Articles:
请记住通过将其包装在CDATA部分中来转义所有HTML内容。
因此,只需使用content:encoded
和<![CDATA[
包装]]>
的HTML内容:
<content:encoded><![CDATA[
<!doctype html>
...
</html>
]]></content:encoded>
此外:
$content = $item->addChild('content:encoded');
$html = $content->addChild('html');
上面的代码生成以下XML:<encoded><html/></encoded>
使用以下内容更改这些行:
$content = $item->addChild('content:encoded', null, 'http://purl.org/rss/1.0/modules/content/');
$base = dom_import_simplexml($content);
$docOwner = $base->ownerDocument;
$base->appendChild($docOwner->createCDATASection('<html>Some HTML</html>'));
生成以下有效的XML元素:
<content:encoded><![CDATA[<html>Some HTML</html>]]></content:encoded>
如需参考,请查看:
答案 1 :(得分:0)
我解决了我自己这里的代码如下:
$rssfeed = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$rssfeed .= '<rss version="2.0">';
$rssfeed .= '<channel>';
$rssfeed .= '<title>My RSS feed</title>';
$rssfeed .= '<link>my link</link>';
$rssfeed .= '<description>something</description>';
$rssfeed .= '<language>en-us</language>';
$rssfeed .= '<copyright>Copyright (C) 2017</copyright>';
foreach ($list as $l) {
$rssfeed .= '<item>';
$rssfeed .= '<title>' . $l['titre'] . '</title>';
$rssfeed .= '<description>' . $l['contenu'] . '</description>';
$rssfeed .= '<link>' . "my link" . '</link>';
$rssfeed .= '<pubDate>' . date("D, d M Y H:i:s O", strtotime($l['date_de_creation'])) . '</pubDate>';
$rssfeed .= '</item>';
}
$rssfeed .= '</channel>';
$rssfeed .= '</rss>';
$handle = fopen("../rss.xml", "w+");
fclose($handle);
$myfile = fopen("../rss.xml", "w");
fwrite($myfile, $rssfeed);
fclose($myfile);