【php中soap】php中SOAP WebService的wsdl文件生成类

更新时间:2019-07-01    来源:WebService    手机版     字体:

【www.bbyears.com--WebService】

代码如下

 代码如下

class SoapDiscovery {

    private $class_name = "";
    private $service_name = "";

    /**
     * SoapDiscovery::__construct() SoapDiscovery class Constructor.
     *
     * @param string $class_name
     * @param string $service_name
     * */
    public function __construct($class_name = "", $service_name = "") {
        $this->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 = "service_name . "Port">";
        $bindingWSDL = "service_name . "Binding" type="tns:" . $this->service_name . " port\"="">\n\n";
        $serviceWSDL = "service_name . " \"="">\n\nservice_name . "Port" binding="tns:" . $this->service_name . "Binding\">\n\n\n";
        $messageWSDL = "";
        foreach ($methods as $method) {
            if ($method->isPublic() && !$method->isConstructor()) {
                $portTypeWSDL.= "getName() . " \"="">\n" . "getName() . " request\"="">\ngetName() . "Response\" />\n\n";
                $bindingWSDL.= "getName() . " \"="">\n" . "service_name . "#" . $this->class_name . "#" . $method->getName() . " \"="">\nservice_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n\n\nservice_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n\n\n";
                $messageWSDL.= "getName() . " request\"="">\n";
                $parameters = $method->getParameters();
                foreach ($parameters as $parameter) {
                    $messageWSDL.= "getName() . " \"="" type="\"xsd:string\"">\n";
                }
                $messageWSDL.= "
\n";
                $messageWSDL.= "getName() . " response\"="">\n";
                $messageWSDL.= "getName() . " \"="" type="\"xsd:string\"">\n";
                $messageWSDL.= "
\n";
            }
        }
        $portTypeWSDL.= "
\n";
        $bindingWSDL.= "\n";
        return 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";
    }

}

使用方法

 代码如下 $a = new SoapDiscovery();

然后里面的方法与类就可以直接调用了哦。

本文来源:http://www.bbyears.com/asp/55970.html

热门标签

更多>>

本类排行