PHP programming in the use of XML format to transmit data, here to demonstrate the next PHP to send XML as a post, the server receives, and parse the XML process!
post_xml.php Source:
1<?PHP2 Header("content-type:text/html; Charset=utf-8 ");3 //detect if Curl is supported4 if(!extension_loaded(' Curl '))5 {6 Trigger_error(' Sorry, please turn on the CURL function module! ‘,E_user_error);7 }8 //Constructing XML9 $xmldata= <<<XMLTen<?xml version= ' 1.0 ' encoding= ' UTF-8 '?> One<group> A<name> Zhang San </name> -<age>22</age> -</group> the XML; - //initializing a Curl session - $ch=curl_init (); - //Set URL +curl_setopt ($ch, Curlopt_url, ' http://localhost/test/deal_xml.php '); - //Set Send Mode +curl_setopt ($ch, Curlopt_post,true); A //set the data sent atcurl_setopt ($ch, Curlopt_postfields,$xmldata); - //crawl the URL and pass it to the browser -Curl_exec ($ch); - //Turn off the Curl resource and release the system resources -Curl_close ($ch); -?>
Note: When constructing XML, you must pay attention to proper format, no spaces, etc.
deal_xml.php Source:
1<?PHP2 //receive the transmitted data3 $fileContent=file_get_contents("Php://input");4 //Convert to SimpleXML object5 $xmlResult=simplexml_load_string($fileContent);6 //foreach traversal loop7 foreach($xmlResult->children () as $childItem)8 {9 Echo $childItem->getname (). ' and '.$childItem. ' <br/> ';//output XML node name and valueTen } One?>
Results:
Name-> Zhang San
Age->22
The Curl simulation Post method submits the XML data and parses