PHP interacts with third-party interfaces via post request in XML message format (sending XML, fetching XML, and parsing XML steps)

Source: Internet
Author: User
Tags convert json to array

Developer side: Send requests and receive results

<?php//The following demo, the functions are as follows://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 support for Curlif (!extension_loaded ("curl")) {Trigger_error ("Sorry, please turn on the CURL function module! ", E_user_error);} Construction xml$xmldata= "<?xml version= ' 1.0 ' encoding= ' UTF-8 '?><group><name> Zhang San </name><age> 22</age></group> ";//Initial A curl session $curl = Curl_init ();//Set Urlcurl_setopt ($curl, Curlopt_url," http:// Localhost/demo/dealxml.php ");//Set Send method: Postcurl_setopt ($curl, Curlopt_post, true);//Set Send data curl_setopt ($curl, Curlopt_postfields, $xmldata);//true returns the information obtained by CURL_EXEC () as a string instead of a direct output curl_setopt ($curl, Curlopt_returntransfer, TRUE);//Perform a curl session (the returned data is XML) $return _xml = curl_exec ($curl);//Close the Curl resource and release the system Resource Curl_close ($curl);//echo $return _xml ;//exit;//prohibits referencing external XML entities Libxml_disable_entity_loader (TRUE);//convert XML to SimpleXML object, convert SimpleXML object to JSON, and then convert JSON to array. $value _array = Json_decode (Json_encode (simplexml_load_string ($return _XML, ' simplexmlelement ', Libxml_nocdata)), true); echo "<pre>";p rint_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 entity Libxml_disable_entity_loader (TRUE);//convert XML to SimpleXML object first, then convert SimpleXML object to JSON, then JSON Convert an array. $value _array = Json_decode (Json_encode (simplexml_load_string ($fileContent, ' simplexmlelement ', libxml_nocdata)), true); # # # Gets the value for business processing $name = $value _array[' name ']; $age = $value _array[' age '];//by querying to determine if the user exists # # # Add the query result to the array $value_ array[' result ' = 1;### converts the array to XML format, returns $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;
return $xml;? >

PHP interacts with third-party interfaces via post request in XML message format (send XML, get XML, parse 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.