自动生成和替换站点地图
auto generating and replacing a sitemap
我正在尝试自动替换服务器上的站点地图文件。使用 php 和 mysqli,我生成了所需的输出,但我不知道如何将该输出保存为 .xml 文件。
我读过有关使用 php 创建、打开和写入文件的内容,但我不知道如何获取生成的内容并将其放入文件中。请问有什么指点吗?
到目前为止,这是我的代码...
$my_file = 'sitemap.xml';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
$data=""; //how do I include the code below as my 'data'?
<?php echo"<?xml version=\"1.0\" encoding=\"utf-8\" ?>"; ?>
<?php
include "connectScript.php";
$date = date("Y-m-d");
header("Content-type: text/xml");
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
$baseUrl="mysite.co.uk/page.php";
$query = "SELECT DISTINCT topic FROM db";
$result = $conn->query($query) or die (mysql_error($query));
while($row = $result->fetch_assoc()) {
$topic = $row['topic'];
$topic = "$baseUrl?t=${topic}";
?>
<url>
<loc>http://www.<?php echo $topic; ?></loc>
<lastmod><?php echo $date; ?></lastmod>
<changefreq>daily</changefreq>
<priority>1.00</priority>
</url>
<?php
}
?>
</urlset>
<?php
fwrite($handle, $data);
?>
尝试这样的事情:
$my_file = 'sitemap.xml';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
$data=""; //how do I include the code below as my 'data'?
<?php ob_start();?> //start the output buffer
<?php echo"<?xml version=\"1.0\" encoding=\"utf-8\" ?>"; ?>
<?php
include "connectScript.php";
$date = date("Y-m-d");
//header("Content-type: text/xml");//remove this, this isn't an xml file it's a php file creating xml
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
$baseUrl="mysite.co.uk/page.php";
$query = "SELECT DISTINCT topic FROM db";
$result = $conn->query($query) or die (mysql_error($query));
while($row = $result->fetch_assoc()) {
$topic = $row['topic'];
$topic = "$baseUrl?t=${topic}";
?>
<url>
<loc>http://www.<?php echo $topic; ?></loc>
<lastmod><?php echo $date; ?></lastmod>
<changefreq>daily</changefreq>
<priority>1.00</priority>
</url>
<?php
}
?>
</urlset>
<?php
$data = ob_get_clean(); // set everything that was output above to the $data variable
fwrite($handle, $data);
?>
有几种方法可以将数据保存到文件中,最简单的方法之一是(引用自手册)
int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )
这只是 fopen/fwrite/fclose 的简化版本。但是,它确实会像您注意到的那样写入一个字符串。
获取字符串中的内容
选项 A
可以使用以下方法构建字符串:
$string = "Hello I'm a string.";
要追加更多内容,您可以使用
$string = $string . " And I'm another part.";
或使用 assignment operator 的较短版本:
$string .= " And I'm another part.";
选项 B
也可以使用 ob_start 缓冲任何输出(打印 (print()/echo/etc.) 的内容,如下所示:
ob_start();
echo "Hello i'm a string.";
echo "And I'm another part.";
// do whatever more you need.
$content = ob_get_clean();
file_put_contents('file.txt', $content);
我正在尝试自动替换服务器上的站点地图文件。使用 php 和 mysqli,我生成了所需的输出,但我不知道如何将该输出保存为 .xml 文件。
我读过有关使用 php 创建、打开和写入文件的内容,但我不知道如何获取生成的内容并将其放入文件中。请问有什么指点吗?
到目前为止,这是我的代码...
$my_file = 'sitemap.xml';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
$data=""; //how do I include the code below as my 'data'?
<?php echo"<?xml version=\"1.0\" encoding=\"utf-8\" ?>"; ?>
<?php
include "connectScript.php";
$date = date("Y-m-d");
header("Content-type: text/xml");
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
$baseUrl="mysite.co.uk/page.php";
$query = "SELECT DISTINCT topic FROM db";
$result = $conn->query($query) or die (mysql_error($query));
while($row = $result->fetch_assoc()) {
$topic = $row['topic'];
$topic = "$baseUrl?t=${topic}";
?>
<url>
<loc>http://www.<?php echo $topic; ?></loc>
<lastmod><?php echo $date; ?></lastmod>
<changefreq>daily</changefreq>
<priority>1.00</priority>
</url>
<?php
}
?>
</urlset>
<?php
fwrite($handle, $data);
?>
尝试这样的事情:
$my_file = 'sitemap.xml';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
$data=""; //how do I include the code below as my 'data'?
<?php ob_start();?> //start the output buffer
<?php echo"<?xml version=\"1.0\" encoding=\"utf-8\" ?>"; ?>
<?php
include "connectScript.php";
$date = date("Y-m-d");
//header("Content-type: text/xml");//remove this, this isn't an xml file it's a php file creating xml
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
$baseUrl="mysite.co.uk/page.php";
$query = "SELECT DISTINCT topic FROM db";
$result = $conn->query($query) or die (mysql_error($query));
while($row = $result->fetch_assoc()) {
$topic = $row['topic'];
$topic = "$baseUrl?t=${topic}";
?>
<url>
<loc>http://www.<?php echo $topic; ?></loc>
<lastmod><?php echo $date; ?></lastmod>
<changefreq>daily</changefreq>
<priority>1.00</priority>
</url>
<?php
}
?>
</urlset>
<?php
$data = ob_get_clean(); // set everything that was output above to the $data variable
fwrite($handle, $data);
?>
有几种方法可以将数据保存到文件中,最简单的方法之一是(引用自手册)
int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )
这只是 fopen/fwrite/fclose 的简化版本。但是,它确实会像您注意到的那样写入一个字符串。
获取字符串中的内容
选项 A
可以使用以下方法构建字符串:
$string = "Hello I'm a string.";
要追加更多内容,您可以使用
$string = $string . " And I'm another part.";
或使用 assignment operator 的较短版本:
$string .= " And I'm another part.";
选项 B
也可以使用 ob_start 缓冲任何输出(打印 (print()/echo/etc.) 的内容,如下所示:
ob_start();
echo "Hello i'm a string.";
echo "And I'm another part.";
// do whatever more you need.
$content = ob_get_clean();
file_put_contents('file.txt', $content);