龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php wsdl生成类,无需任何组件

时间:2014-07-22 14:48来源: 作者: 点击:
分享到:
这个类是国外一个网站找来的,无需任何组件,即可实现wsdl接口处理还不错的,之前弄的一个小说网站http://www.fuheishu.com/ 与.net简单实现对话. 下面的代码一共三个,处理类、服务端、客
这个类是国外一个网站找来的,无需任何组件,即可实现wsdl接口处理

还不错的,之前弄的一个小说网站http://www.fuheishu.com/ 与.net简单实现对话. 

下面的代码一共三个,处理类、服务端、客户端;调用自己的wsdl文件即可;
<?php
/*
* 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 = "<?xml version=\"1.0\" ?>\n";
		$headerWSDL.= "<definitions name=\"$this->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.= "<types xmlns=\"http://schemas.xmlsoap.org/wsdl/\" />\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 = '<portType name="'.$this->service_name.'Port">';
		$bindingWSDL = '<binding name="'.$this->service_name.'Binding" type="tns:'.$this->service_name."Port\">\n<soap:binding style=\"rpc\" transport=\"http://schemas.xmlsoap.org/soap/http\" />\n";
		$serviceWSDL = '<service name="'.$this->service_name."\">\n<documentation />\n<port name=\"".$this->service_name.'Port" binding="tns:'.$this->service_name."Binding\"><soap:address location=\"http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF']."\" />\n</port>\n</service>\n";
		$messageWSDL = '';
		foreach ($methods as $method) {
			if ($method->isPublic() && !$method->isConstructor()) {
				$portTypeWSDL.= '<operation name="'.$method->getName()."\">\n".'<input message="tns:'.$method->getName()."Request\" />\n<output message=\"tns:".$method->getName()."Response\" />\n</operation>\n";
				$bindingWSDL.= '<operation name="'.$method->getName()."\">\n".'<soap:operation soapAction="urn:'.$this->service_name.'#'.$this->class_name.'#'.$method->getName()."\" />\n<input><soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</input>\n<output>\n<soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</output>\n</operation>\n";
			    $messageWSDL.= '<message name="'.$method->getName()."Request\">\n";
				$parameters = $method->getParameters();
				foreach ($parameters as $parameter) {
					$messageWSDL.= '<part name="'.$parameter->getName()."\" type=\"xsd:string\" />\n";
				}
				$messageWSDL.= "</message>\n";
				$messageWSDL.= '<message name="'.$method->getName()."Response\">\n";
				$messageWSDL.= '<part name="'.$method->getName()."\" type=\"xsd:string\" />\n";
				$messageWSDL.= "</message>\n";
			}
		}
		$portTypeWSDL.= "</portType>\n";
		$bindingWSDL.= "</binding>\n";
		return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>');
	}
	
	/**
	 * SoapDiscovery::getDiscovery() Returns discovery of WSDL.
	 * 
	 * @return string
	 **/
	public function getDiscovery() {
		return "<?xml version=\"1.0\" ?>\n<disco:discovery xmlns:disco=\"http://schemas.xmlsoap.org/disco/\" xmlns:scl=\"http://schemas.xmlsoap.org/disco/scl/\">\n<scl:contractRef ref=\"http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF']."?wsdl\" />\n</disco:discovery>";
	}
}

?>

2. [代码]wsdl服务端接收     跳至 [1] [2] [3] [全屏预览]

<?php
/**
 +------------------------------------------------------------------------------
 * wsdl服务端  
 +------------------------------------------------------------------------------
 * @wsdl服务端接收
 +------------------------------------------------------------------------------
 */


define('WSDL_URL','hello.wsdl');        //定义WSDL文件路径
ini_set('soap.wsdl_cache_enabled','0');    //关闭WSDL缓存
 
 //WSDL文件不存在时自动创建
if(!file_exists(WSDL_URL))
{
    require_once 'SoapDiscovery.class.php'; //引入类
    $disco = new SoapDiscovery('Mywsdl','www.fuheishu.com');
    $str = $disco->getWSDL();
    file_put_contents(WSDL_URL,$str);
}
 
//SOAP开启并接收Client传入的参数响应 
$server = new SoapServer(WSDL_URL);
$server->setClass('Mywsdl');
$server->handle();
 
 
//测试定义公开的类
class Mywsdl {
    private $nombre = '';
    public function __construct($name = 'World') 
	{
		$this->name = $name;
	}
    public function greet($name = '') 
	{
		$name = $name?$name:$this->name;
        return 'Hello '.$name.'.';
	}
    public function serverTimestamp() 
	{
		return time();
	}
	public function myfunc($a=''){
	    return $a;
	}
}
 
?>

3. [代码]wsdl客户端     跳至 [1] [2] [3] [全屏预览]

<?php
/**
 +------------------------------------------------------------------------------
 * wsdl客户端  
 +------------------------------------------------------------------------------
 * @wsdl客户端发送
 +------------------------------------------------------------------------------
 */
 
$client = new SoapClient("http://127.0.0.1/createsoap/hello.wsdl");
 
try {
        $result = $client->myfunc('789');
        var_dump($result);
        //echo "The answer isresult";
}
catch (SoapFault $f){
        echo "Error Message: {$f->getMessage()}";
}
?>
精彩图集

赞助商链接