我将一些代码拼凑在一起,它可能可以写得更好——但它确实有效,而且我学会了不要对函数式代码挑剔。无论如何,我这里有一些代码会自动更新 sitemap.xml 文件和“问题——我在这里发布问题的原因”一个 RSS 提要文件。
写入 sitemap.xml 文件效果很好!我很满意。这很容易,因为它在文件底部写入新数据并通过写入</urlset>
.
但是,对于 rss 提要文件,我需要将新项目放在顶部。我似乎无法弄清楚如何做到这一点。所以,这里附上我的代码的当前副本。基本上,标题为 RSS FEED WRITER 的第二部分是标题为 SITEMAP.XML WRITER 的第一部分的副本。我需要如何将代码更改为第二部分,以便将新项目直接写入此行下方的顶部:<atom:link href="http://mydomainname.com/index.rss" rel="self" type="application/rss+xml" />
在 index.rss 文件上。
非常感谢任何愿意帮助我的人。下面是代码:
//
// 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);