我希望看到通过电子邮件发布的XML数据。
过程:
1)XML POST是针对PHP脚本的。
2)PHP脚本只需通过print_r($_POST, true)
将$ _POST数据作为邮件发送给我。
我正在使用PHPMailer和SimpleXML来实现这一目标。
这是我发布XML的代码:
$xml = new SimpleXMLElement('<lead/>');
$post_body = array_flip($template['post_body']);
array_walk_recursive($post_body, array($xml, 'addChild'));
$stream_options = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/xml\r\n",
'content' => $xml->asXML(),
),
);
$context = stream_context_create($stream_options);
$response = file_get_contents($template['address'], null, $context);
$post_body
是一个如下所示的数组:
Array
(
[testbuyer] => buyerName
[tdunn5e@hatena.ne.jp] => testEmail
[Timothy] => leadFirstName
[world] => customTest2
)
以下是我发送电子邮件的代码:
try
{
$mailer = new PHPMailer();
$mailer->isSMTP();
$mailer->Host = EMAIL_HOST;
$mailer->Port = EMAIL_PORT;
$mailer->SMTPAuth = true;
$mailer->Username = EMAIL_USERNAME;
$mailer->Password = EMAIL_PASSWORD;
$mailer->setFrom(EMAIL_ADDRESS);
$mailer->addAddress('myemail@gmail.com');
$mailer->Subject = 'Test POST';
$mailer->Body = print_r($_POST, true);
$mailer->send();
$response = 'Mail sent OK';
}
catch (phpmailerException $e)
{
$response = $e->errorMessage();
}
catch (Exception $e)
{
$response = $e->getMessage();
}
我收到的邮件很好,但邮件只显示一个空数组。它看起来像这样:
Array
(
)
为什么会这样?我只想在消息中看到XML,以便我可以验证它的正常工作。任何帮助将不胜感激。