将特定的PHP数据保存到XML

时间:2014-08-29 09:31:58

标签: php xml

我似乎无法从我的PHP获取特定数据,以使用我的网络表单输出到XML文件。

我做错了什么?

<?php

$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$notes = $_POST['notes'];
$attn = $_POST['attn'];

if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,"."))) 
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
die ("Go back! ! ");
}

if(empty($visitor) || empty($visitormail) || empty($notes )) {
echo "<h2>Use Back - fill in all fields</h2>\n";
die ("Use back! ! ");
}

$todayis = date("l, F j, Y, g:i a");

$attn = $attn ;
$subject = $attn;

$notes = stripcslashes($notes);

$message = "
Subject: $attn \n
Message: $notes \n 
From: $visitor ($visitormail)\n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
";

$from = "From: $visitormail\r\n";


mail('my@email.com', $subject, $message, $from);

?>

<?php

$xml = new SimpleXMLElement('');

    $mydata = $xml->addChild('VisitorInfo');
    $mydata->addChild('Visitor',$visitor);
    $mydata->addChild('Key',$ip);

    $mydata->PHP_EOL;

mysql_close($db);

$fp = fopen("VisitorData.xml","wb");

fwrite($fp,$xml->asXML());

fclose($fp);

?>

错误代码我收到:

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 2: parser error : Start tag expected, '<' not found in /home/content/48/10101748/html/sendeail.php on line 57

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Subject: adfasdfd in /home/content/48/10101748/html/sendeail.php on line 57

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/content/48/10101748/html/sendeail.php on line 57

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/content/48/10101748/html/sendeail.php:57 Stack trace: #0 /home/content/48/10101748/html/sendeail.php(57): SimpleXMLElement->__construct('?Subject: adfas...') #1 {main} thrown in /home/content/48/10101748/html/sendeail.php on line 57

1 个答案:

答案 0 :(得分:1)

SimpleXMLElement的PHP文档中,第一个值必须是string类型,他的数据应该是&#34;格式良好的XML字符串或XML文档的路径或URL&#34 ;

目前,您还没有填写这些条件。您应该创建一个新节点,例如:

$xml = new SimpleXMLElement('<xml/>');
$mydata = $xml->addChild('VisitorInfo');
$mydata->addChild('Visitor','toto');
$mydata->addChild('Key', '1');

$mydata->PHP_EOL; // Don't understand the goal here

此代码示例的输出:

SimpleXMLElement Object
(
    [Visitor] => toto
    [Key] => 1
)

此外,您在代码中使用mysql_*。考虑切换到mysqli_*PDO。从PHP文档: &#34; 警告:自PHP5.5.0起,此扩展程序已弃用,将来将被删除。相反,应该使用MySQLi或PDO MySQL扩展。&#34;