简体   繁体   English

在PHP中,从基于RDF的RSS Feed创建ATOM Feed

[英]In PHP, create an ATOM Feed from an RDF based RSS Feed

Because of a legacy system I have that produces an old RDF based RSS 1.0 Feed, and the inability of most of the RSS Readers to handle HTTP Basic Auth, I'd like to have a PHP script that reads this feed and produces an ATOM feed from it (as I have a nice reader here that can handle HTTP Auth, looks nice, but that sadly cannot cope with RSS 1.0). 由于我有一个旧系统,该系统会生成基于RDF的旧版RSS 1.0 Feed,并且大多数RSS阅读器无法处理HTTP Basic Auth,因此我希望有一个PHP脚本读取此Feed并生成ATOM Feed从它(因为我这里有一个很好的阅读器,可以处理HTTP认证,看起来不错,但可悲的是不能与RSS 1.0应付)。

Googling around for some time, I pretty much didn't find a lot. 谷歌搜索了一段时间,我几乎没有找到很多东西。 This is the code I tried right now, but the XSLT doesn't work, and I don't know anything about XSLT), and I got it from here . 这是我现在尝试的代码,但是XSLT不起作用,而且我对XSLT一无所知),我是从这里得到的。 Getting behind the HTTP Basic Auth already worked, but I'll leave it in there: 落后于HTTP基本身份验证已经有效,但我将其保留在其中:

$https_user = "thisismyhttpbasicusername";
$https_password = "thisismyhttpbasicpassword";
$https_server = "sometld.tld/dokuwiki/feed.php";

$opts = array('http' =>
  array(
    'method'  => 'GET',
    'header'  => "Content-Type: text/xml\r\n".
      "Authorization: Basic ".base64_encode("$https_user:$https_password")."\r\n",
    'content' => $body,
    'timeout' => 60
  )
);

$context  = stream_context_create($opts);
$url = 'http://'.$https_server;
$xml = file_get_contents($url, false, $context, -1, 40000);
$xsl = file_get_contents("http://sometld.tld/minitools/rdf2atom.xslt");
$xslDoc = new DOMDocument();
$xslDoc->loadXML($xsl);
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);
$proc = new XSLTProcessor();
$proc->importStylesheet($xslDoc);
echo $proc->transformToXML($xmlDoc);

This is the XSLT file: 这是XSLT文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<items>
  <xsl:copy-of select="//item">
    <xsl:apply-templates/>
  </xsl:copy-of>
</items>
</xsl:template>
</xsl:stylesheet>

The output should be all elements, so I can wrap them with a element and have it be read by an RSS Reader that doesn't handle RSS 1.0 anymore. 输出应该是所有元素,所以我可以用一个元素包裹他们并经RSS阅读器不处理RSS 1.0不再被读取。

The RSS that is produced by the system looks like this: 系统生成的RSS如下所示:

<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> ... </channel>
  <item rdf:about="http://sometld.tld/dokuwiki/doku.php?id=interessante_und_hilfreiche_links&amp;rev=1368016122&amp;do=diff">
    <dc:format>text/html</dc:format>
    <dc:date>2013-05-08T14:28:42+02:00</dc:date>
    <dc:creator>akku</dc:creator>
    <title>interessante_und_hilfreiche_links</title>
    <link>http://sometld.tld/dokuwiki/doku.php?id=interessante_und_hilfreiche_links&amp;rev=1368016122&amp;do=diff</link>
    <description>
*  .NET Framework Setup Verification Tool &lt;- This .NET Framework setup verification tool is designed to automatically perform a set of steps to verify the installation state of one or more versions of the .NET Framework on a computer.  It will verify the presence of files, directories, registry keys and values for the .NET Framework.  It will also verify that simple applications that use the .NET Framework can be run correctly.</description>
  </item>
  <item>... more items ... </item>
</rdf:RDF>

Do you know a PHP based script that can transform the RSS 1.0 to an Atom formatted feed? 您是否知道基于PHP的脚本,可以将RSS 1.0转换为Atom格式的提要? Or can you correctify the XSLT I use? 还是可以纠正我使用的XSLT? For reference, the actual output right now looks like this: 作为参考,实际输出现在看起来是这样的:

<?xml version="1.0"?>
<items/>

This is most probably an namespace issue. 这很可能是名称空间问题。 Try to add: 尝试添加:

xmlns="http://purl.org/rss/1.0/"

as namespace to your xslt stylesheet. 作为xslt样式表的名称空间。

For example the following xslt: 例如以下xslt:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:rss="http://purl.org/rss/1.0/">
    <xsl:template match="/">
        <items>
            <xsl:copy-of select="//rss:item" />
        </items>
    </xsl:template>
</xsl:stylesheet>

Will generate the following output: 将生成以下输出:

<?xml version="1.0"?>
<items xmlns:rss="http://purl.org/rss/1.0/">
    <item 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/" rdf:about="http://sometld.tld/dokuwiki/doku.php?id=interessante_und_hilfreiche_links&amp;rev=1368016122&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2013-05-08T14:28:42+02:00</dc:date>
        <dc:creator>akku</dc:creator>
        <title>interessante_und_hilfreiche_links</title>
        <link>http://sometld.tld/dokuwiki/doku.php?id=interessante_und_hilfreiche_links&amp;rev=1368016122&amp;do=diff</link>
        <description>
            *  .NET Framework Setup Verification Tool &lt;- This .NET Framework setup verification tool is designed to automatically perform a set of steps to verify the installation state of one or more versions of the .NET Framework on a computer.  It will verify the presence of files, directories, registry keys and values for the .NET Framework.  It will also verify that simple applications that use the .NET Framework can be run correctly.
        </description>
    </item>
    <item 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/">... more items ... </item>
</items>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM