2015-09-15 10:05

使用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:

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

Here is my code which sends the email:

    $mailer = new PHPMailer();
    $mailer->Host = EMAIL_HOST;
    $mailer->Port = EMAIL_PORT;
    $mailer->SMTPAuth = true;
    $mailer->Username = EMAIL_USERNAME;
    $mailer->Password = EMAIL_PASSWORD;
    $mailer->Subject = 'Test POST';
    $mailer->Body = print_r($_POST, true);      

    $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:


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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答