php wsdl生成类

来源:互联网 发布:qq点亮图标软件 编辑:程序博客网 时间:2024/06/05 16:02

[1].[代码] wsdl处理类 跳至 [1] [2] [3]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/*
* wsdl处理类
*/ 
classSoapDiscovery {
    private$class_name = '';
    private$service_name = '';
     
    /**
     * SoapDiscovery::__construct() SoapDiscovery class Constructor.
     *
     * @param string $class_name
     * @param string $service_name
     **/
    publicfunction __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
     **/
    publicfunction getWSDL() {
        if(empty($this->service_name)) {
            thrownew 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)) {
            thrownew Exception('No class name.');
        }
         
        $class= newReflectionClass($this->class_name);
         
        if(!$class->isInstantiable()) {
            thrownew 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($methodsas $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($parametersas $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";
        returnsprintf('%s%s%s%s%s%s',$headerWSDL,$portTypeWSDL,$bindingWSDL,$serviceWSDL,$messageWSDL,'</definitions>');
    }
     
    /**
     * SoapDiscovery::getDiscovery() Returns discovery of WSDL.
     *
     * @return string
     **/
    publicfunction 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]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?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= newSoapDiscovery('Mywsdl','www.fuheishu.com');
    $str= $disco->getWSDL();
    file_put_contents(WSDL_URL,$str);
}
  
//SOAP开启并接收Client传入的参数响应
$server= newSoapServer(WSDL_URL);
$server->setClass('Mywsdl');
$server->handle();
  
  
//测试定义公开的类
classMywsdl {
    private$nombre = '';
    publicfunction __construct($name= 'World')
    {
        $this->name = $name;
    }
    publicfunction greet($name= '')
    {
        $name= $name?$name:$this->name;
        return'Hello '.$name.'.';
    }
    publicfunction serverTimestamp()
    {
        returntime();
    }
    publicfunction myfunc($a=''){
        return$a;
    }
}
  
?>

[3].[代码] wsdl客户端 跳至 [1] [2] [3]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
/**
 +------------------------------------------------------------------------------
 * wsdl客户端 
 +------------------------------------------------------------------------------
 * @wsdl客户端发送
 +------------------------------------------------------------------------------
 */
  
$client= newSoapClient("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()}";
}
?>
0 0
原创粉丝点击