The Curl simulation Post method submits the XML data and parses

Source: Internet
Author: User
Tags php programming

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.