Home > Article > Backend Development > php handles wsdl
0x00 Preface
I have been writing interfaces recently. Before this, interface data transmission was transmitted or obtained using json or xml format. But this time when jointly debugging with a third party, they provided the wsdl format. Changed to SB in an instant...
Google the test code, and the test calls the third-party interface to return status 200. I thought it would be over if nothing happened, but when I got closer, I discovered that no matter how I called their interface, the correct data was not returned. Later, after checking the log, they found that they had not received the passed parameters at all. They were confused for an afternoon and into the evening before they solved the problem. I thought it was quite interesting, so I wrote it down first.
0x01 What is wsdl? Based on what someone said, it is an xml format document used to describe the definition of Web Server, that is to say, it 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/xwebservice s/testServer?wsdl', what ends like this? When setting the URL of wsdl, a bunch of xml structured data will be given to you.
That's right, it's just a lump...
Next, how to understand it and the method it says is the key, everything else is useless.
0x02 Understanding the description document
When I first looked at this xml document, I was confused, but it became much more obvious after using the PHP extension.
<?php $client = new SoapClient('http://api.test.cn/xwebservices/testServer?wsdl'); print "\n提供的方法\n"; print_r($client->__getFunctions()); print "相关的数据结构\n"; print_r($client->__getTypes()); print "\n\n";
Here we use the SOAP extension. This extension is the operation processing WebServer service extension provided in the official PHP copy. In the end, we also use it to realize parameter transmission.
As can be understood from the above picture, this interface provides three methods, namely:
xxxxUserInfo
xxxxResumeNum
download**
The relevant data structure refers to the parameter name and parameter type in the method . For example, the xxxxUserInfo method requires three string type parameters. Correspond to in0, in1 and in2 respectively.
Note
The parameter key here must be in0, which is an arbitrary parameter name that does not require an array, user-defined or agreed upon by both parties. When I started writing the interface method, I performed the transmission based on the parameter descriptions given in the interface copy, such as: err_msg (indicating error information), err_code (indicating error coding), and date (final data transmitted). Then change it to an ordered array and fill in the corresponding parameters one by one. At this time, the key is 0 to 2. But after trying it, it still didn't work. Finally, with the mentality of giving it a try, I tried using int0 as the key name and the corresponding err_msg content as the value. OK~, perfect solution.
Code:
<?php /** * @author 0x584A * 获取WSDL接口数据 */ class getwsdlTest extends PHPUnit_Framework_TestCase { public $apiurl = 'http://api.test.cn/xwebservices/testServer?wsdl'; private static $soapClientHandler; private $infoArr = [ 'err_msg' => 'false', 'err_code' => '0', 'date' => '此处是要传输的数据' ]; public function setUp() { $client = new SoapClient('http://api.test.cn/xwebservices/testServer?wsdl'); print "提供的方法\n"; print_r($client->__getFunctions()); print "相关的数据结构\n"; print_r($client->__getTypes()); print "\n\n"; } /** * xxxxUserInfo方法 */ 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()->synchUserInfo($parameter); //调用结果返回异常 if (!$result instanceof stdClass) { throw new Exception("调用synchUserInfo结果出现异常:" . json_encode($result)); } //调用接口状态码,输出对应错误详情 if ($result->out == '01') { throw new Exception("调用synchUserInfo=>error:" . $result->out . ",msg:接口数据异常"); } $xml_parser = xml_parser_create(); if (!xml_parse($xml_parser, $result->out, true)) { xml_parser_free($xml_parser); throw new Exception("调用synchUserInfo返回的不是一个xml结构体"); } xml_parser_free($xml_parser); //XXE libxml_disable_entity_loader(true); $xml = simplexml_load_string($result->out, 'SimpleXMLElement', LIBXML_NOCDATA); // 输出参数 var_dump($xml->data); echo " 成功".PHP_EOL; } catch (SoapFault $soapFault) { throw new Exception($soapFault->getMessage() . $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; } } ?>