Php processing wsdl

Source: Internet
Author: User
Php processing wsdl

0x00 preface

I have been writing interfaces recently. Before that, the interface data transmission was in json or xml format. But in this joint debugging with a third party, they gave the wsdl format. Instantly changes SB...

Google to test code, test call third-party interface return status 200. I thought it would be okay to end it. I found that no matter how I call their interface, there is no correct data echo. Then they looked at the log and found that they did not receive the parameters that were passed in, and the problem was solved only one afternoon and one night later. I think it's interesting, so let's take a look at it first.

0x01 what is wsdl?

According to a certain degree, it is an xml document used to describe the definition of Web Server, that is, a Web Server method and parameter description.

See: http://baike.baidu.com/link? Url = R7x3FdekxbndR4SlzQLZE_2m1ebpt_SWt9IMjoHSErvLlbZ3-hwhR3ERrinXS1xZaDvkYFpxWnUchrk34_WkZq

When we request http://api.test.cn/xwebservices/testServer? Wsdl ', similar to the end? In the wsdl URL, a piece of xml structure data is sent to you.

That's right...

Next, the key is how to understand it and the method it describes, and the rest are useless.

0x02 understanding description

At the beginning, reading this xml document was quite awesome, but it was much more obvious with PHP extensions.

 _ GetFunctions (); print "related data structure \ n"; print_r ($ client->__ getTypes (); print "\ n ";

Here we use the SOAP extension, which is used to show the WebServer service extension in the official PHP text, and finally we use it to implement parameter transmission.

As shown in the preceding figure, this interface provides three methods:

XxxxUserInfo

XxxxResumeNum

Download **

The relevant data organization refers to the parameter name and parameter type in the method. For example, the xxxxUserInfo method requires three string-type parameters. In0, in1, and in2.

Note

The parameter key here must be in0, that is, a parameter name that does not require an array, which is customized or agreed by both parties. When I start writing an interface method, I use the parameter descriptions in the interface text, such as err_msg (indicating the error message) and err_code (indicating the error code ), date (the final data transmitted. Then it is changed to an ordered array, and the corresponding parameters are filled in one by one. in this case, the key is 0 to 2. After you try it, you still have nothing to use. In the end, we try to use int0 as the key name and the corresponding err_msg content as the value. OK ~, Perfect solution.

Code:

 'False', 'Err _ code' => '0', 'date' => 'here is the data to be transmitted ']; public function setUp () {$ client = new SoapClient (' http://api.test.cn/xwebservices/testServer?wsdl '); Print "provided Method \ n"; print_r ($ client->__ getFunctions (); print "related data structure \ n "; print_r ($ client->__ getTypes (); print "\ n";}/*** xxxxUserInfo method */public function testxxxxUserInfoData () {try {$ ApiInfo = $ this-> infoArr; // set request param $ parameter = array ('in0' => $ ApiInfo ['err _ msg '], 'in1' => $ ApiInfo ['err _ Code'], 'in2' => $ ApiInfo ['Date']); $ result = $ this-> getSoapClientHandler () -> synchUse RInfo ($ parameter); // The Call result returns an exception if (! $ Result instanceof stdClass) {throw new Exception ("An Exception occurred when calling synchUserInfo :". json_encode ($ result);} // call the interface status code and output the corresponding error details if ($ result-> out = '01 ') {throw new Exception ("call synchUserInfo => error :". $ result-> out. ", msg: Interface Data exception") ;}$ xml_parser = xml_parser_create (); if (! Xml_parse ($ xml_parser, $ result-> out, true) {xml_parser_free ($ xml_parser); throw new Exception ("calling synchUserInfo does not return an xml struct ");} xml_parser_free ($ xml_parser); // XXE libxml_disable_entity_loader (true); $ xml = simplexml_load_string ($ result-> out, 'simplexmlelement', LIBXML_NOCDATA ); // output parameter var_dump ($ xml-> data); echo "success ". PHP_EOL;} catch (SoapFault $ soapFault) {throw new Exception ($ soapFault-> get Message (). $ this-> getSoapClientHandler ()->__ getLastResponse () ;}/ *** @ description getSoapClientHandler */public function getSoapClientHandler () {if (! Self ::$ soapClientHandler) {self ::$ soapClientHandler = new SoapClient ($ this-> getSynchApi ();} return self ::$ soapClientHandler ;} /*** @ description getSynchApi */public function getSynchApi () {return $ this-> apiurl ;}}?>

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.