PHP生成XML,获取文档末尾的Extra内容

标签 php xml

只是尝试使用 PHP 生成一个简单的 XML 文档,我发现了一个似乎有效的示例,我怎么会收到此错误:

This page contains the following errors:

error on line 5 at column 1: Extra content at the end of the document Below is a rendering of the page up to the first error.

这是我的 PHP 代码:

$xml = new SimpleXMLElement("<xml/>");
$form_data = array(
    "name" => $_POST['name'],
    "email" => $_POST['email'],
    "message" => $_POST['message'],
    "reg" => $_POST['reg'],
    "image" => $image_location
);

$xml->addChild('uploaded',serialize($_POST));
$xml->addChild('imageerror',"imageerror");

header('Content-type: text/xml');
print($xml->asXML());

如果我查看我的页面源代码,这是我得到的:

<?xml version="1.0"?>
<xml><uploaded>a:0:{}</uploaded><imageerror>imageerror</imageerror></xml>

<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

所以它似乎确实正确地生成了 XML,但是虚拟主机添加了一些废话,这就是导致错误的原因吗?如果是这样,唯一的方法就是停止更改虚拟主机?

最佳答案

有 3 种方法可以做到这一点:

1) 他们的 cPanel 中总是有一个链接,可以禁用您代码中的分析代码。大多数时候文件的链接是 Your_web_host_url/analytics.php 。但始终要考虑到,这样做你就是在否认他们的政策,他们最终会删除你的帐户,这会导致你的数据丢失。

2) 您可以在 php 脚本末尾使用 exit 命令。

3) 在您的 public_html 文件夹中创建一个 .htaccess 并添加以下内容 代码:

<FilesMatch "\.(php)$">
php_value auto_append_file none
</FilesMatch> 

.php 替换为您不想在其中进行分析的任何类型的文件。

关于PHP生成XML,获取文档末尾的Extra内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32553613/

相关文章:

php - 如何获取数字的整数部分和小数部分?

android - 强制按钮停留在电话屏幕的底部

java - 使用反射从 XML 文件(Java 中)读取数据

javascript - 如何在模态中传递 php 值? Bootstrap 3,

php - foreach 行插入 foreach

php - 仅显示 Mysql 数据库中的月份

xml - 如何在 powershell 中打印 xml 元素的实际内容?

javascript - 我可以在没有 HTML 的浏览器中从 XML 启动 javascript 吗?

java - 为什么我会收到此错误(未知标记 < :color> )even when there's no such tag in the file?

php - 来自 PHP/Apache2 的响应中缺少 CORS header