如何编写新的RSS项目行到顶部

时间:2012-09-01 22:04:05

标签: php rss fopen fwrite fseek

我将一些代码拼凑在一起,它可能写得更好 - 但它有效并且我学会了不要使用功能代码挑剔。无论如何,我在这里有一些代码将自动更新sitemap.xml文件和“问题 - 我在这里发布问题的原因”RSS源文件。

写入sitemap.xml文件非常有效!我很满意。这很简单,因为它在文件底部写入新数据,并通过编写</urlset>来关闭。

然而,使用rss feed文件,我需要新的项目才能登顶。我似乎无法想象如何做到这一点。因此,附在此处是我的代码的CURRENT副本。基本上标题为RSS FEED WRITER的第二部分是标题为SITEMAP.XML WRITER的第一部分的副本。我需要将代码更改为第二部分,以便将新项目直接写入此行的顶部:index.rss文件中的<atom:link href="http://mydomainname.com/index.rss" rel="self" type="application/rss+xml" />

非常感谢所有愿意帮助我的人。这是下面的代码:

//
// SITEMAP.XML WRITER
//
$sitemapurl = "<url>";
$sitemaploc = "<loc>";
$sitemaplocation = "http://mydomainname.com/$url.php";
$sitemaploc1 = "</loc>";
$sitemappri = "<priority>";
$sitemappriority = "1.0";
$sitemappri1 = "</priority>";
$sitemapcha = "<changefreq>";
$sitemapchange = "daily";
$sitemapcha1 = "</changefreq>";
$sitemapurl1 = "</url>";

$sitemapurlset = "\n</urlset>";
$sitemaplength_end_string = strlen($sitemapurlset);

$sitemapfp =
fopen("sitemap.xml","r+") or die("can't open file");
fseek($sitemapfp, -$sitemaplength_end_string, SEEK_END); 
fwrite ($sitemapfp, "\n".$sitemapurl."\n".$sitemaploc."".$sitemaplocation."".$sitemaploc1."\n".$sitemappri."".$sitemappriority."".$sitemappri1."\n".$sitemapcha."".$sitemapchange."".$sitemapcha1."\n".$sitemapurl1."".$sitemapurlset."\n"); //added url set to here
      fclose($sitemapfp); 

//
// RSS FEED WRITER
//

$rssitem = "<item>";
$rsstitle = "<title>$title</title>";
$rsslink = "<link>http://mydomainname.com/$url.php</link>";
$rssguid = "<guid isPermalink=\"true\">http://mydomainname.com/$url.php</guid>";
$rsspubdate = "<pubDate>".date('D, d M Y H:i:s O')."</pubDate>";
$rssdescription = "<description>$metadescription</description>";
$rssitem1 = "</item>";
$rsschannelrssset = "\n</channel>\n</rss>";
$rsslength_end_string = strlen($rsschannelrssset);
$rssfp =
fopen("index.rss","r+") or die("can't open file");
fseek($rssfp, -$rsslength_end_string, SEEK_END);
fwrite ($rssfp, "".$rssitem."\n".$rsstitle."\n".$rsslink."\n".$rssguid."\n".$rsspubdate."\n".$rssdescription."\n".$rssitem1."\n".$rsschannelrssset."\n");
fclose($rssfp);

编辑:添加index.rss文件的顶部:

<?xml version="1.0" encoding="utf-8"?>
<rss version='2.0' xmlns:lj='http://www.livejournal.org/rss/lj/1.0/' xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>My Domain Name RSS Feed</title>
<description>Description of My Domain Name RSS Feed</description>
<link>http://mydomainname.com/</link>
<atom:link href="http://mydomainname.com/index.rss" rel="self" type="application/rss+xml" />

然后我尝试了这段代码,但它不起作用:

//
// RSS FEED WRITER
//

$rssitem = "<item>";
$rsstitle = "<title>$title</title>";
$rsslink = "<link>http://mydomainname.com/$url.php</link>";
$rssguid = "<guid isPermalink=\"true\">http://mydomainnanme.com/$url.php</guid>";
$rsspubdate = "<pubDate>".date('D, d M Y H:i:s O')."</pubDate>";
$rssdescription = "<description>$metadescription</description>";
$rssitem1 = "</item>";
$rssatomlink = "<atom:link href=\"http://mydomainname.com/index.rss\" rel=\"self\" type=\"application/rss+xml\" />";
$rsslength_end_string = strlen($rssatomlink);
$rssfp =
fopen("index.rss","r+") or die("can't open file");
fseek($rssfp, -$rsslength_end_string, SEEK_END);
fwrite ($rssfp, "".$rssitem."\n".$rsstitle."\n".$rsslink."\n".$rssguid."\n".$rsspubdate."\n".$rssdescription."\n".$rssitem1."\n");
fclose($rssfp);

1 个答案:

答案 0 :(得分:0)

查看PHP Universal Feed Generator。它支持RSS 1.0 / 2.0和ATOM 1.0。