Home > Backend Development > PHP Tutorial > php handles wsdl

php handles wsdl

高洛峰
Release: 2016-10-20 14:18:58
Original
1658 people have browsed it

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...

php handles wsdlNext, 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(&#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";
Copy after login

php handles wsdlHere 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 = &#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;
    }

}
?>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template