用PHP创建一个XML站点地图。

时间:2022-11-22 01:10:41

I'm trying to create a sitemap that will automatically update. I've done something similiar with my RSS feed, but this sitemap refuses to work. You can view it live at http://designdeluge.com/sitemap.xml I think the main problem is that its not recognizing the PHP code. Here's the full source:

我正在尝试创建一个站点地图,它将自动更新。我用RSS提要做了一些类似的事情,但是这个站点地图拒绝工作。您可以在http://designdeluge.com/sitemap.xml上实时查看它,我认为主要的问题是它没有识别PHP代码。这是完整的资料来源:

 <?php 


include 'includes/connection.php';

header("Content-type: text/xml");

echo '<?xml version="1.0" encoding="UTF-8" ?>';

?>

<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">

    <url>
        <loc>http://designdeluge.com/</loc>
        <lastmod>2010-04-20</lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.00</priority>
    </url>

    <url>
        <loc>http://designdeluge.com/about.php</loc>
        <lastmod>2010-04-20</lastmod>
        <changefreq>never</changefreq>
        <priority>0.5</priority>
    </url>

    <?php

    $entries = mysql_query("SELECT * FROM Entries");

    while($row = mysql_fetch_assoc($entries)) {
    $title = stripslashes($row['title']);
    $date = date("Y-m-d", strtotime($row['timestamp']));

    echo "

    <url>
        <loc>http://designdeluge.com/".$title."</loc>
        <lastmod>".$date."</lastmod>
        <changefreq>never</changefreq>
        <priority>0.8</priority>
    </url>";

 } ?>

</urlset>

The problem is that the dynamic URL's (e.g. the ones pulled from the DB) aren't being generated and the sitemap won't validate. Thanks!

问题是动态URL(例如从DB中提取的URL)没有生成,sitemap也不会验证。谢谢!

EDIT: Right now, I'm just trying to get the code itself working. I have it set up as a PHP file on my local testing server. The code above is being used. Right now, nothing displays nothing on screen or in the source. I'm thinking I made a syntax error, but I can't find anything. Any and all help is appreciated!

编辑:现在,我只是想让代码本身工作。我将它作为PHP文件设置在本地测试服务器上。上面的代码正在被使用。目前,在屏幕或源代码中没有显示任何内容。我在想我犯了一个语法错误,但是我什么都找不到。非常感谢您的帮助!

EDIT 2: Ok, I got it sorted out guys. Apparently, I had to echo the xml declaration with PHP. The final code is posted above. Thanks for your help!

编辑2:好的,我把它整理好了。显然,我必须用PHP回显xml声明。最后的代码发布在上面。谢谢你的帮助!

3 个解决方案

#1


28  

If you take a look at the sitemap.xml that's generated (using view source, in your browser, for example), you'll see this :

如果你看一下网站地图。生成的xml(例如,使用浏览器中的view source),您将看到:

<?php header('Content-type: text/xml'); ?>
<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http:/
...

The <?php, present in that output, shows that PHP code is not interpreted.

< ?在输出中显示的php显示php代码没有被解释。


This is probably because your webserver doesn't recognize .xml as an extension of files that should contain PHP code.

这可能是因为您的web服务器不承认.xml是应该包含PHP代码的文件的扩展。

At least two possible solutions :

至少有两种可能的解决办法:

  • Re-configure your server, so XML files go through the PHP interpreter (might not be such a good idea : that can cause problems with existing files ! )
  • 重新配置服务器,使XML文件通过PHP解释器(这可能不是一个好主意:这会导致现有文件出现问题!)
  • Change the extension of your sitemap, to sitemap.php for example, so it's interpreted by your server.
  • 将您的站点地图扩展到站点地图。例如,它是由你的服务器解释的。


I would add another solution :

我要补充另一种解决办法:

  • Have a sitemap.php file, that contains the code
  • 有一个网站地图。php文件,包含代码
  • And use a RewriteRule so the sitemap.xml URL actually points to the sitemap.php file
  • 使用一个RewriteRule网站地图。xml URL实际上指向站点地图。php文件

With that, you'll have the sitemap.xml URL, which is nice (required ? ), but as the code will be in sitemap.php, it'll get interpreted.

有了它,你就有了站点地图。xml URL,很好(需要吗?)但是因为代码将在sitemap中。php,它会得到解释。

See Apache's mod_rewrite.

看到Apache的mod_rewrite。

#2


3  

I've used William's code (thanks) and with a few small modifications it worked for me.

我使用了William的代码(谢谢),并对它做了一些小小的修改。

I think the line:

我认为:

header("Content-type: text/xml");

should be the second line after the top <?php

应该是顶部

Incidentally, just a small point to anyone else that copies it, but there is a single space character before the <?php in the first line - if you inadvertantly copy it as I did, you will spend a bit of time trying to figure out why the code won't work for you!

顺便说一下,对于复制它的其他人来说,只是一个小点,但是在

I had to tweak the MySql select statement a little bit too.

我也需要稍微调整一下MySql select语句。

Finally, in the output, I have used a variable $domain so that this piece of code can be used as a template without the need to think about it (provided you use the same table name each time). The variabe is added to the connectdb.php file which is included to connect to the database.

最后,在输出中,我使用了一个变量$domain,以便可以将这段代码用作模板,而无需考虑它(只要每次使用相同的表名)。变量被添加到connectdb。php文件,用于连接数据库。

Here is my working version of the William's code:

这是我的工作版本的威廉的代码:

<?php 
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" ?>';
include 'includes/connectdb.php';
?>

<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">

    <url>
        <loc>http://www.DOMAIN.co.uk/</loc>
        <priority>1.00</priority>
    </url>

    <?php

    $sql = "SELECT * FROM pages WHERE onshow = 1 ORDER BY id ASC";
    $result = mysql_query($sql,$conn);      
    while($row = mysql_fetch_array($result))
    { 
    $filename = stripslashes($row['filename']);
    ?>
    <url>
        <loc>http://www.<?php echo "$domain"; ?>/<?php echo "$filename" ?></loc>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>

 <?php } ?>

</urlset>

#3


3  

The best solution is to add to your apache .htaccess file the following line after RewriteEngine On

最好的解决方案是在RewriteEngine启动后将以下一行添加到apache .htaccess文件中

RewriteRule ^sitemap\.xml$ sitemap.php [L]

and then simply having a file sitemap.php in your root folder that would be normally accessible via http://yoursite.com/sitemap.xml, the default URL where all search engines will firstly search.

然后有一个文件站点地图。在根文件夹中的php,通常可以通过http://yoursite.com/sitemap.xml访问,这是所有搜索引擎首先搜索的默认URL。

The file sitemap.php shall start with

网站地图的文件。php将开始

<?php header('Content-type: application/xml; charset=utf-8') ?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>

it works :)

工作原理:)

#1


28  

If you take a look at the sitemap.xml that's generated (using view source, in your browser, for example), you'll see this :

如果你看一下网站地图。生成的xml(例如,使用浏览器中的view source),您将看到:

<?php header('Content-type: text/xml'); ?>
<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http:/
...

The <?php, present in that output, shows that PHP code is not interpreted.

< ?在输出中显示的php显示php代码没有被解释。


This is probably because your webserver doesn't recognize .xml as an extension of files that should contain PHP code.

这可能是因为您的web服务器不承认.xml是应该包含PHP代码的文件的扩展。

At least two possible solutions :

至少有两种可能的解决办法:

  • Re-configure your server, so XML files go through the PHP interpreter (might not be such a good idea : that can cause problems with existing files ! )
  • 重新配置服务器,使XML文件通过PHP解释器(这可能不是一个好主意:这会导致现有文件出现问题!)
  • Change the extension of your sitemap, to sitemap.php for example, so it's interpreted by your server.
  • 将您的站点地图扩展到站点地图。例如,它是由你的服务器解释的。


I would add another solution :

我要补充另一种解决办法:

  • Have a sitemap.php file, that contains the code
  • 有一个网站地图。php文件,包含代码
  • And use a RewriteRule so the sitemap.xml URL actually points to the sitemap.php file
  • 使用一个RewriteRule网站地图。xml URL实际上指向站点地图。php文件

With that, you'll have the sitemap.xml URL, which is nice (required ? ), but as the code will be in sitemap.php, it'll get interpreted.

有了它,你就有了站点地图。xml URL,很好(需要吗?)但是因为代码将在sitemap中。php,它会得到解释。

See Apache's mod_rewrite.

看到Apache的mod_rewrite。

#2


3  

I've used William's code (thanks) and with a few small modifications it worked for me.

我使用了William的代码(谢谢),并对它做了一些小小的修改。

I think the line:

我认为:

header("Content-type: text/xml");

should be the second line after the top <?php

应该是顶部

Incidentally, just a small point to anyone else that copies it, but there is a single space character before the <?php in the first line - if you inadvertantly copy it as I did, you will spend a bit of time trying to figure out why the code won't work for you!

顺便说一下,对于复制它的其他人来说,只是一个小点,但是在

I had to tweak the MySql select statement a little bit too.

我也需要稍微调整一下MySql select语句。

Finally, in the output, I have used a variable $domain so that this piece of code can be used as a template without the need to think about it (provided you use the same table name each time). The variabe is added to the connectdb.php file which is included to connect to the database.

最后,在输出中,我使用了一个变量$domain,以便可以将这段代码用作模板,而无需考虑它(只要每次使用相同的表名)。变量被添加到connectdb。php文件,用于连接数据库。

Here is my working version of the William's code:

这是我的工作版本的威廉的代码:

<?php 
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" ?>';
include 'includes/connectdb.php';
?>

<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">

    <url>
        <loc>http://www.DOMAIN.co.uk/</loc>
        <priority>1.00</priority>
    </url>

    <?php

    $sql = "SELECT * FROM pages WHERE onshow = 1 ORDER BY id ASC";
    $result = mysql_query($sql,$conn);      
    while($row = mysql_fetch_array($result))
    { 
    $filename = stripslashes($row['filename']);
    ?>
    <url>
        <loc>http://www.<?php echo "$domain"; ?>/<?php echo "$filename" ?></loc>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>

 <?php } ?>

</urlset>

#3


3  

The best solution is to add to your apache .htaccess file the following line after RewriteEngine On

最好的解决方案是在RewriteEngine启动后将以下一行添加到apache .htaccess文件中

RewriteRule ^sitemap\.xml$ sitemap.php [L]

and then simply having a file sitemap.php in your root folder that would be normally accessible via http://yoursite.com/sitemap.xml, the default URL where all search engines will firstly search.

然后有一个文件站点地图。在根文件夹中的php,通常可以通过http://yoursite.com/sitemap.xml访问,这是所有搜索引擎首先搜索的默认URL。

The file sitemap.php shall start with

网站地图的文件。php将开始

<?php header('Content-type: application/xml; charset=utf-8') ?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>

it works :)

工作原理:)