首页 > 解决方案 > 我们如何在 laravel 中创建 Soap 服务器并使用 laravel wsdl url 调用移动应用程序

问题描述

后端创建路由在 laravel 7 中使用 Zendframework 的 zend soap 生成 WSDL 文件

 if(isset($_GET['wsdl'])) {

        // Create wsdl object and register type(s).
        $wsdl = new Wsdl('wsdl', $this->endpoint);

        foreach($this->types as $key => $class) {
            $wsdl->addType($class, $key);
        }

        // Set type(s) on strategy object.
        $this->strategy->setContext($wsdl);

        foreach($this->types as $key => $class) {
            $this->strategy->addComplexType($class);
        }

        // Auto-discover and output xml.
        $discover = new AutoDiscover($this->strategy);
        $discover->setBindingStyle(array('style' => 'document'));
        $discover->setOperationBodyStyle(array('use' => 'literal'));
        $discover->setClass($this->service);
        $discover->setUri($this->endpoint);
        $discover->setServiceName($this->name);
        
        echo $discover->toXml();

    } else {
        
        
        $server = new Server($this->endpoint . '?wsdl');
        // $server = new SoapServer($this->endpoint . '?wsdl', array(
        //     'style' => SOAP_DOCUMENT,
        //     'use' => SOAP_LITERAL,
        // ));
        // $server->setObject(new DocumentLiteralWrapped(new $this->service()));
        // $server->handle();
        
        $server->setClass(new DocumentLiteralWrapper(new $this->service()));
        $server->registerFaultException($this->exceptions);
        $server->setOptions($this->options);

        // Intercept response, then decide what to do with it.
        $server->setReturnResponse(true);
        
        $response = $server->handle();
        
        // Deal with a thrown exception that was converted into a SoapFault.
        // SoapFault thrown directly in a service class bypasses this code.
        if ($response instanceof SoapFault) {

            $output->headers->set("Status", 500);
            return self::serverFault($response);
            
        } else {
            return $response;
         //   echo $response;
            
        }

 }

laravel 路由生成 wsdl 在 laravel 中用于创建肥皂客户端,但它无法加载 wsdl,当我们用于移动应用程序时,它返回异常“HTTP 请求失败,HTTP 状态:500”

使用 zend soap 生成 WSDL 标记顺序(与 zoap 代码相同)

 <types>   </types>
   <portType> </portType>
   <service> </service>
   <message> </message> 

它提供带有#method名称的肥皂动作,我们将其用于移动设备

** 移动应用代码**

 private val NAMESPACE = "http://10.2.0.114:8001/api/server/account-action";
 private val METHODNAME = "withdrawDetail"
 private val WSDL = "http://10.2.0.114:8001/api/server/account-action?wsdl";
 private val SOAP_ACTION = "$NAMESPACE#$METHODNAME"
 private val TAG = "soap"


var responseDump = ""

try {

val envelope = SoapSerializationEnvelope(SoapEnvelope.VER11)
val request = SoapObject(NAMESPACE, METHODNAME)
request.addProperty("withdrawId", "1")
request.addProperty("token", "695E8784AE45B219F62C4EBE21E3E")

val headerList: MutableList = ArrayList()
headerList.add(HeaderProperty("Content-Type", "application/xml; charset=UTF-8"))

headerList.add(
HeaderProperty(
"Authorization",
"Bearer " + "695E8784AE45B219F62C4EBE21E3E"
)
)

headerList.add(
HeaderProperty(
"Content-Type", "application/xml"
)
)

headerList.add(HeaderProperty("soapAction", NAMESPACE))
envelope.bodyOut = request
val transport = HttpTransportSE(WSDL)
transport.debug = true

try {
transport.call(SOAP_ACTION, envelope, headerList)
val requestDump = transport.requestDump
responseDump = transport.responseDump
Log.e(TAG, responseDump)

} catch (e: IOException) {
e.printStackTrace()
}
} catch (e: Exception) {
e.printStackTrace()
}
return responseDump
}

标签: androidlaravelkotlinksoap2zend-soap

解决方案


推荐阅读