dsykx64220 2015-09-15 10:05
浏览 1051

使用Content-type:application / xml发送POST数据

I want to see XML data that is being POSTed via email.

The process:

1) XML POST is made to a PHP script.

2) The PHP script simply mails me the $_POST data via print_r($_POST, true) as the message.

I am using PHPMailer and SimpleXML to accomplish this.

Here is my code which POSTs the 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
",
       'content' => $xml->asXML(),
    ),
);
$context  = stream_context_create($stream_options);
$response = file_get_contents($template['address'], null, $context);

$post_body is an array which looks like this:

Array
(
    [testbuyer] => buyerName
    [tdunn5e@hatena.ne.jp] => testEmail
    [Timothy] => leadFirstName
    [world] => customTest2
)

Here is my code which sends the email:

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();
}

I receive the email fine, but the message just shows an empty array. It looks like this:

Array
(
)

Why is this happening? I just want to see the XML in the message so I can verify its all working properly. Any help would be appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 关于#python#的问题:自动化测试