PHP is interacting with third-party interfaces through XML messages (sending XML, getting XML, and parsing XML steps)

Source: Internet
Author: User

  

Developer side: Send requests and receive results

<?PHP//The following demo, the implementation of the following functions://1-developers need to determine whether a user exists, to request a third-party interface. 2-Communication with a third-party interface that transmits data in an XML format. The developer sends the user information in XML format to the third-party interface//3-third party interface gets the developer's XML data, through the data query, and then sends the result in the XML format to the developer. First detect if curl is supportedif(!extension_loaded("Curl")) {    Trigger_error("Sorry, please turn on the Curl function module!) ",E_user_error);}//Constructing XML$xmldata= "<?xml version= ' 1.0 ' encoding= ' UTF-8 '?><group><name> Zhang San </name><age>22</age> </group> ";//initial One Curl session$curl=curl_init ();//Set URLcurl_setopt ($curl, Curlopt_url, "http://localhost/demo/dealxml.php");//Set Send mode: Postcurl_setopt ($curl, Curlopt_post,true);//Set Send datacurl_setopt ($curl, Curlopt_postfields,$xmldata);//TRUE to return the information obtained by CURL_EXEC () as a string instead of a direct outputcurl_setopt ($curl, Curlopt_returntransfer,TRUE);//perform a Curl session (the returned data is XML)$return _xml= Curl_exec ($curl);//Turn off the Curl resource and release the system resourcesCurl_close ($curl);//Echo $return _xml;//exit;//prohibit referencing external XML entitiesLibxml_disable_entity_loader (true);//Convert the XML to a SimpleXML object, convert the SimpleXML object to JSON, and then convert the JSON to an array. $value _array= Json_decode (Json_encode (simplexml_load_string($return _xml, ' SimpleXMLElement ', libxml_nocdata),true);Echo"<pre>";Print_r($value _array);?>

Third-party interface: receives the request and returns the processing result

<?PHP//receive the transmitted data$fileContent=file_get_contents("Php://input"); ## # Convert XML to an array//prohibit referencing external XML entitiesLibxml_disable_entity_loader (true);//Convert the XML to a SimpleXML object, convert the SimpleXML object to JSON, and then convert the JSON to an array. $value _array= Json_decode (Json_encode (simplexml_load_string($fileContent, ' SimpleXMLElement ', libxml_nocdata),true);## # Get the value for business processing$name=$value _array[' Name '];$age=$value _array[' Age '];//determine if the user exists by querying## # Add the query results to the array$value _array[' result '] = 1;## # Convert the array to XML format, return$xml= "<?xml version= ' 1.0 ' encoding= ' UTF-8 '?><group>";foreach($value _array  as $key=$val){    if(Is_numeric($val)){        $xml. = "<".$key." > ".$val." </".$key." > "; }Else{        $xml. = "<".$key." ><! [cdata[].$val."]] ></".$key." > "; }}$xml. = "</group>";Echo $xml;?>

PHP is interacting with third-party interfaces through XML messages (sending XML, getting XML, and parsing XML steps)

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.