简体   繁体   English

自动生成和替换站点地图

[英]auto generating and replacing a sitemap

I am trying to automatically replace my sitemap file on the server. 我正在尝试自动替换服务器上的站点地图文件。 Using php and mysqli, I generate the required output, but I cannot work out how to save that output as a .xml file. 使用php和mysqli,我生成所需的输出,但是无法计算出如何将该输出另存为.xml文件。
I've read about creating, opening and writing files with php, but I cannot work out how to take my generated content and get it into a file. 我已经读过关于使用php创建,打开和写入文件的信息,但是我无法弄清楚如何将生成的内容带入文件中。 Any pointers please? 有指针吗?

Here is my code so far... 到目前为止,这是我的代码...

 $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("Ymd"); 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); ?> 

Try something like this: 尝试这样的事情:

$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);
?>

There are a few ways to save data to a file, one of the most simple ones being (as quoted from the manual) 有几种方法可以将数据保存到文件中,最简单的方法之一是(如手册中所述)

int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )

Which is just a simplified version of fopen/fwrite/fclose. 这只是fopen / fwrite / fclose的简化版本。 It does however write a string as you had noticed. 但是,确实会像您注意到的那样写一个字符串。

Getting things in a string 把东西串起来

Option A 选项A

Building a string can be done by using: 可以使用以下命令来构建字符串:

$string = "Hello I'm a string.";

To append more you can use 要追加更多内容,您可以使用

$string = $string . " And I'm another part.";

Or the shorter version using an assignment operator : 或更短的版本使用赋值运算符

$string .= " And I'm another part.";

Option B 选项B

It is also possible to buffer any output (things printed (print()/echo/etc.) using ob_start like so: 也可以使用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);

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

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