Home  >  Article  >  Backend Development  >  How does php handle wsdl

How does php handle wsdl

藏色散人
藏色散人forward
2019-12-03 14:24:293134browse

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. It changes 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 has been said in a certain degree, 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://m.sbmmt.com/faq/437443.html

When we request http://api.test.cn/xwebservices /testServer?wsdl', similar to this URL ending with ?wsdl, a bunch of xml structured data will be given to you.

Yes, it’s just a lump...

How does php handle wsdl

Next, how to understand it and the method it says is the key, Everything else is in vain.

0x02 Understanding the description document

It was quite confusing when I first looked at this xml document, but it became much clearer after using the PHP extension.

<?php
$client = new SoapClient(&#39;http://api.test.cn/xwebservices/testServer?wsdl&#39;);
print "\n提供的方法\n";
print_r($client->__getFunctions());
print "相关的数据结构\n";
print_r($client->__getTypes());
print "\n\n";

How does php handle wsdl

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, is 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 = &#39;http://api.test.cn/xwebservices/testServer?wsdl&#39;;
    private static $soapClientHandler;
    private $infoArr = [
        &#39;err_msg&#39; => &#39;false&#39;,
        &#39;err_code&#39; => &#39;0&#39;,
        &#39;date&#39; => &#39;此处是要传输的数据&#39;
    ];
    public function setUp()
    {
        $client = new SoapClient(&#39;http://api.test.cn/xwebservices/testServer?wsdl&#39;);
        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(
                &#39;in0&#39; => $ApiInfo[&#39;err_msg&#39;],
                &#39;in1&#39; => $ApiInfo[&#39;err_code&#39;],
                &#39;in2&#39; => $ApiInfo[&#39;date&#39;]
            );
            $result = $this->getSoapClientHandler()->synchUserInfo($parameter);
            //调用结果返回异常
            if (!$result instanceof stdClass) {
                throw new Exception("调用synchUserInfo结果出现异常:" . json_encode($result));
            }
            //调用接口状态码,输出对应错误详情
            if ($result->out == &#39;01&#39;) {
                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, &#39;SimpleXMLElement&#39;, 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;
    }
}
?>

Promote: "PHP Tutorial"

The above is the detailed content of How does php handle wsdl. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete