【发布时间】:2017-01-24 14:23:52
【问题描述】:
我正在使用 PHP 来动态生成 XML 站点地图。生成部分运行顺利,但不知何故无法将结果保存为 XML 文件。这是我写的 PHP:
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?><urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"/>');
$node = $xml->addChild('url');
$node->addChild('loc', 'http://www.pb.com/blog/');
$node->addChild('changefreq', "daily");
$node->addChild('priority', "1");
$connect = dbconn(PROJHOST, POSTSDB, POSTSUSR, POSTSPWD);
$sql = "SELECT * FROM tblposts ORDER BY id";
$query = $connect->prepare($sql);
if($query->execute()) {
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
if($rows){
foreach($rows as $row){
$post_date = $row['post_date'];
$node = $xml->addChild('url');
$node->addChild('loc', trim("http://www.pb.com/blog/" . $row['post_name']));
$node->addChild('lastmod', str_replace(' ', 'T', $row['post_date']) . "-05:00");
$node->addChild('changefreq', "weekly");
$node->addChild('priority', "0.6");
}
}
}
Header('Content-type: text/xml');
print($xml->asXML());
// $dom->preserveWhiteSpace = FALSE;
$xml->save('sitemap.xml');
它抛出的致命错误是:
调用未定义的方法 SimpleXMLElement::save() in /xxx/xxx/xxx/sandboxgenerate_sitemap.php 在第 79 行
还有其他选择吗?
【问题讨论】:
-
当你遇到这样的问题时 Read the flippin Manual. Thats what its there for 保存方法在哪里?无处
-
哇。没有找借口,但粗鲁并没有真正的理由。不过感谢您的帮助。
-
使用 PHP 中的
file_put_contents()函数写入文件。 -
@TheLearner 真的,看手册比输入问题花费的时间更少