PHP 在 XML 开始标签上抛出错误
PHP throwing error on XML opening tag
我有一个文件,其中包含一个名为 sitemap.html
的文件,在站点地图中,我有以下代码:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc><?php echo SITE_URL . '/'; ?></loc>
<changefreq>monthly</changefreq>
<priority>1.00</priority>
</url>
<?php foreach ($this->data->content as $content): ?>
<url>
<loc><?php echo SITE_URL . $content->permalink; ?></loc>
<lastmod><?php echo date("Y-m-d", $content->publish_date); ?></lastmod>
<changefreq>monthly</changefreq>
<priority><?php echo $content->sitemap_index; ?></priority>
</url>
<?php endforeach; ?>
</urlset>
一切似乎都很好,但我收到错误 500,内容是:
PHP Parse error: syntax error, unexpected 'version' (T_STRING) in sitemap.html on line 1
如您所见,我使用的是 <?xml
而不是 <?php
那么为什么要将其解析为 PHP?
起初,我以为是魔术引号引起了问题,但是当我执行 get_magic_quotes_gpc() 的 var_dump 时,我得到 bool(false)
,所以魔术引号不是连上了。
此外,在我的 php.ini
中,我看到 magic_quotes
是 OFF
。
作为修复它的替代方法,我将第一行替换为以下内容:
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
但我仍然很好奇发生了什么。
我在同一台服务器上托管了其他网站(PHP 版本 5.4.45),但我没有在其他网站的站点地图上看到 PHP 解析错误...
知道是什么导致了这个错误吗?
您应该检查 short_open_tag
选项。在我看来,PHP 将您的 <?xml
的 <?
部分视为 php 开放标记并生成错误。
如前所述,这是由于 short_open_tag option
Tells PHP whether the short form (<? ?>
) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?>
inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"?>'; ?>
. Also, if disabled, you must use the long form of the PHP open tag (<?php ?>
).
如果您要 PHP 处理 HTML 页面并且它在所有其他站点上工作,那么您(显然)正在处理一个难以解决的配置问题。
您可以继续搜索隐藏在服务器某处的 short_open_tags
设置,或者您可以使用 PHP.
简单地回显 XML 声明
<?php echo '<?xml version="1.0" encoding="UTF-8" ?>' ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc><?php echo SITE_URL . '/'; ?></loc>
<changefreq>monthly</changefreq>
<priority>1.00</priority>
</url>
<?php foreach ($this->data->content as $content): ?>
<url>
<loc><?php echo SITE_URL . $content->permalink; ?></loc>
<lastmod><?php echo date("Y-m-d", $content->publish_date); ?></lastmod>
<changefreq>monthly</changefreq>
<priority><?php echo $content->sitemap_index; ?></priority>
</url>
<?php endforeach; ?>
</urlset>
这是关于该主题的旧页面:
PHP opening tags and XML declaration
我更愿意为所有 PHP 代码文件提供 .php 扩展名,这样很明显它们包含代码并且不会产生每个 [=21] 处理一次 PHP 的开销=] 文件,但这只是我的意见。
我有一个文件,其中包含一个名为 sitemap.html
的文件,在站点地图中,我有以下代码:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc><?php echo SITE_URL . '/'; ?></loc>
<changefreq>monthly</changefreq>
<priority>1.00</priority>
</url>
<?php foreach ($this->data->content as $content): ?>
<url>
<loc><?php echo SITE_URL . $content->permalink; ?></loc>
<lastmod><?php echo date("Y-m-d", $content->publish_date); ?></lastmod>
<changefreq>monthly</changefreq>
<priority><?php echo $content->sitemap_index; ?></priority>
</url>
<?php endforeach; ?>
</urlset>
一切似乎都很好,但我收到错误 500,内容是:
PHP Parse error: syntax error, unexpected 'version' (T_STRING) in sitemap.html on line 1
如您所见,我使用的是 <?xml
而不是 <?php
那么为什么要将其解析为 PHP?
起初,我以为是魔术引号引起了问题,但是当我执行 get_magic_quotes_gpc() 的 var_dump 时,我得到 bool(false)
,所以魔术引号不是连上了。
此外,在我的 php.ini
中,我看到 magic_quotes
是 OFF
。
作为修复它的替代方法,我将第一行替换为以下内容:
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
但我仍然很好奇发生了什么。
我在同一台服务器上托管了其他网站(PHP 版本 5.4.45),但我没有在其他网站的站点地图上看到 PHP 解析错误...
知道是什么导致了这个错误吗?
您应该检查 short_open_tag
选项。在我看来,PHP 将您的 <?xml
的 <?
部分视为 php 开放标记并生成错误。
如前所述,这是由于 short_open_tag option
Tells PHP whether the short form (
<? ?>
) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use<?xml ?>
inline. Otherwise, you can print it with PHP, for example:<?php echo '<?xml version="1.0"?>'; ?>
. Also, if disabled, you must use the long form of the PHP open tag (<?php ?>
).
如果您要 PHP 处理 HTML 页面并且它在所有其他站点上工作,那么您(显然)正在处理一个难以解决的配置问题。
您可以继续搜索隐藏在服务器某处的 short_open_tags
设置,或者您可以使用 PHP.
<?php echo '<?xml version="1.0" encoding="UTF-8" ?>' ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc><?php echo SITE_URL . '/'; ?></loc>
<changefreq>monthly</changefreq>
<priority>1.00</priority>
</url>
<?php foreach ($this->data->content as $content): ?>
<url>
<loc><?php echo SITE_URL . $content->permalink; ?></loc>
<lastmod><?php echo date("Y-m-d", $content->publish_date); ?></lastmod>
<changefreq>monthly</changefreq>
<priority><?php echo $content->sitemap_index; ?></priority>
</url>
<?php endforeach; ?>
</urlset>
这是关于该主题的旧页面: PHP opening tags and XML declaration
我更愿意为所有 PHP 代码文件提供 .php 扩展名,这样很明显它们包含代码并且不会产生每个 [=21] 处理一次 PHP 的开销=] 文件,但这只是我的意见。