Transfer HTTP POST to xml with php
I have a XML file which should be contained HTTP POST request, something like:
<SMS>
<MSG>HERE SHOULD BE HTTP POST REQUEST</MSG>
<NUMER>123456789</NUMBER>
<USERNAME>Admin</USERNAME>
<SMS>
How can I transfer the HTTP POST request to this specific XML file? I don't have any background with wor开发者_JAVA百科king on XML file :\
Thank you.
There are many ways to do this, but you should be able to do it as simple as the following:
<?php
// Your post data
$post_msg = $_POST['message'];
$post_number = $_POST['number'];
$post_username = $_POST['username'];
// Building your XML string
$strXML = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
$strXML .= '<SMS>'."\n";
$strXML .= '<MSG>'.$post_msg.'</MSG>'."\n";
$strXML .= '<NUMBER>'.$post_number.'</NUMBER>'."\n";
$strXML .= '<USERNAME>'.$post_username.'</USERNAME>'."\n";
$strXML .= '<SMS>'."\n";
// Print your XML
echo $strXML;
?>
Save it as your_file_name.php and it should output as an XML document on the post event.
Assuming the POST keys are valid as XML tags, something like:
<?php
header( 'Content-Type: text/xml' );
echo '<?xml version="1.0" ?>' ;
?>
<SMS>
<?php
foreach( $_POST as $key => $val ) {
echo "<$key>$val</$key>\n" ;
}
?>
</SMS>
精彩评论