php如何处理wsdl-php教程

资源魔 36 0
0x00 媒介

近期不断正在写接口,正在此以前接口数据传输都是应用json或许xml格局进行传输或猎取的。但此次以及第三方联调时,他们给予的是wsdl格局。霎时秒变SB...

谷歌到测试用code,测试挪用第三方接口前往状态200。认为没啥事了能够就此完结了,跟近后才发现,不论怎样挪用他们接口就是不正确的数据回显。随后他们何处查看log后发现,压根传过来的参数他们不接纳到,懵逼了一下战书到早晨才处理了这个成绩。感觉挺无意思的,以是记上去先。

0x01 wsdl是甚么

综合某度上所说,它就是一个xml格局的文档,用于形容Web Server的界说,也就是说是一个Web Server办法及参数阐明。

详见:https://www.php.cn/faq/437443.html

当咱们申请http://api.test.cn/xwebservices/testServer?wsdl',相似这类末端是?wsdl的URL时,会一坨xml构造的数据给你。

没错,就是一坨...

28421c3fc7d991f4c4487b03993a819.png

接上去,怎样看懂它以及它所说的办法才是要害,其它都是白搭。

0x02 了解形容文档

刚开端看这个xml文档时是比拟懵逼的,但用PHP的扩大解决下就显著不少了。

<?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";

05653125ef9f7976837243150044a23.png

这里咱们用到了SOAP扩大,这个扩大是PHP民间案牍中出示操作解决WebServer效劳扩大,终极咱们也是经过它来完成参数传输。

正在下面的图片中能够了解出,这个接口提供了三个办法,辨别是:

● xxxxUserInfo

● xxxxResumeNum

● download**

相干的数据机构则是指办法中参数称号,及参数类型。比方xxxxUserInfo办法,需求三个string类型的参数。辨别对应in0,in1以及in2。

此处的传参数key必定是in0,也就是一个无需数组,用户自界说或单方商定好的恣意参数称号。正在开端写接口办法的时分,我是依据接口案牍中给予的参数阐明如:err_msg(示意谬误信息),err_code(示意谬误编码),date(传输的终极数据)进行传输的。后改成有序数组,挨个填入对应参数,此时key就是的0到2。可试过之后仍是没甚么卵用,终极包着试一试的心态,测验考试一下将int0作为键名,对应的err_msg内容作为值。ok~,完满处理。

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;
    }
}
?>

推动:《PHP教程》

以上就是php若何解决wsdl的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学 wsdl

抱歉,评论功能暂时关闭!