php中soap webservice的wsdl文件生成类是一段老外写的代码非常的简单好用了并且我用了很久没碰到过什么问题,希望对各位有帮助。
代码如下
代码如下 复制代码
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();
然后里面的方法与类就可以直接调用了哦。