laravel中soapServer支持wsdl的例子

$server = new \SoapServer(null, ['uri' => 'noganluonguri']);
$server->setObject(new NganluongServer());
ob_start();
$server->handle();
return ob_get_clean();

上边这段代码是无wsdl模式下的,但是这次是对接第三方的服务,需要我们这边去定义soap webservice,第三方来调用,第三方定义的是wsdl模式的,所以今天研究了下。

laravel代码示例(其它框架类似思考方式):

  • 主要逻辑代码 - SoapService.php
  • 创建路由
$api->any('soapUrl', 'SoapCallbackController@soapFun');
getWSDL());
                    fclose($file);
                }
                $server = new \SoapServer($storagePath . '/wsdl/' . $className . '.wsdl', array('soap_version' => SOAP_1_2));
                $server->setClass($procClass);
                $server->handle();
            } catch (\Exception $e) {
                Log::error('wsdl服务创建异常');
            }
        }
    }
  • 生成wsdl类 - SoapDiscovery.php
class_name = $class_name;
        $this->service_name = $service_name;
    }

    /**
     * SoapDiscovery::getWSDL() Returns the WSDL of a class if the class is instantiable.
     * 
     * @return string
     * */
    public function getWSDL() {
        if (empty($this->service_name)) {
            throw new Exception('No service name.');
        }
        $headerWSDL = "\n";
        $headerWSDL.= "service_name\" targetNamespace=\"urn:$this->service_name\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tns=\"urn:$this->service_name\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns=\"http://schemas.xmlsoap.org/wsdl/\">\n";
        $headerWSDL.= "\n";

        if (empty($this->class_name)) {
            throw new Exception('No class name.');
        }

        $class = new ReflectionClass($this->class_name);

        if (!$class->isInstantiable()) {
            throw new Exception('Class is not instantiable.');
        }

        $methods = $class->getMethods();

        $portTypeWSDL = '';
        $bindingWSDL = '\n\n";
        $serviceWSDL = '\n\nservice_name . 'Port" binding="tns:' . $this->service_name . "Binding\">\n\n\n";
        $messageWSDL = '';
        foreach ($methods as $method) {
            if ($method->isPublic() && !$method->isConstructor()) {
                $portTypeWSDL.= '\n" . '\ngetName() . "Response\" />\n\n";
                $bindingWSDL.= '\n" . '\nservice_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n\n\nservice_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n\n\n";
                $messageWSDL.= '\n";
                $parameters = $method->getParameters();
                foreach ($parameters as $parameter) {
                    $messageWSDL.= '\n";
                }
                $messageWSDL.= "\n";
                $messageWSDL.= '\n";
                $messageWSDL.= '\n";
                $messageWSDL.= "\n";
            }
        }
        $portTypeWSDL.= "\n";
        $bindingWSDL.= "\n";
        //return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '');
        //生成wsdl文件,将上面的return注释
        $fso = fopen($this->class_name . ".wsdl", "w");
        fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ''));
    }

    /**
     * SoapDiscovery::getDiscovery() Returns discovery of WSDL.
     * 
     * @return string
     * */
    public function getDiscovery() {
        return "\n\n\n";
    }

}

?>
  • webservice测试
getSum(10, 24);
echo $soap->__soapCall('getSum',array(10,24));

如有问题请请出,谢谢支持。QQ273086429

你可能感兴趣的:(php,laravel,soap,webservice)