php webservcie

Posted 零七八碎的资料

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php webservcie相关的知识,希望对你有一定的参考价值。

Service.php

 1 <?php
 2 class Service {    
 3     public function echostr($str) {
 4         return $str . ",soap";
 5     }
 6     public function Add($a, $b) {
 7         return $a + $b;
 8     }    
 9 }
10 
11 try {
12     $server = new SoapServer ( ‘Service.wsdl‘, array (‘soap_version‘ => SOAP_1_2 ) );
13     $server->setClass ( ‘Service‘ ); 
14     $server->handle ();
15 } catch ( SoapFault $f ) {
16     print "<server.php>:".$f->faultString;
17 }
18 
19 ?>

SoapDiscovery.class.php

  1 <?php  
  2     
  3 /**  
  4  * Copyright (c) 2005, Braulio José Solano Rojas  
  5  * All rights reserved.  
  6  *   
  7  * Redistribution and use in source and binary forms, with or without modification, are  
  8  * permitted provided that the following conditions are met:  
  9  *   
 10  *  Redistributions of source code must retain the above copyright notice, this list of  
 11  *  conditions and the following disclaimer.   
 12  *  Redistributions in binary form must reproduce the above copyright notice, this list of  
 13  *  conditions and the following disclaimer in the documentation and/or other materials  
 14  *  provided with the distribution.   
 15  *  Neither the name of the Solsoft de Costa Rica S.A. nor the names of its contributors may  
 16  *  be used to endorse or promote products derived from this software without specific  
 17  *  prior written permission.  
 18  *   
 19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND  
 20  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,  
 21  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF  
 22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE  
 23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
 24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  
 25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT  
 26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;  
 27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  
 28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN  
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
 30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  
 31  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
 32  *   
 33  *  
 34  * @version $Id$  
 35  * @copyright 2005   
 36  */  
 37   
 38 /**  
 39  * SoapDiscovery Class that provides Web Service Definition Language (WSDL).  
 40  *   
 41  * @package SoapDiscovery  
 42  * @author Braulio José Solano Rojas  
 43  * @copyright Copyright (c) 2005 Braulio José Solano Rojas  
 44  * @version $Id$  
 45  * @access public  
 46  **/  
 47 class SoapDiscovery {  
 48     private $class_name = ‘‘;  
 49     private $service_name = ‘‘;  
 50       
 51     /**  
 52      * SoapDiscovery::__construct() SoapDiscovery class Constructor.  
 53      *   
 54      * @param string $class_name  
 55      * @param string $service_name  
 56      **/  
 57     public function __construct($class_name = ‘‘, $service_name = ‘‘) {  
 58         $this->class_name = $class_name;  
 59         $this->service_name = $service_name;  
 60     }  
 61       
 62     /**  
 63      * SoapDiscovery::getWSDL() Returns the WSDL of a class if the class is instantiable.  
 64      *   
 65      * @return string  
 66      **/  
 67     public function getWSDL() {  
 68         if (empty($this->service_name)) {  
 69             throw new Exception(‘No service name.‘);  
 70         }  
 71         $headerWSDL = "<?xml version=\"1.0\" ?>\n";  
 72         $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";  
 73         $headerWSDL.= "<types xmlns=\"http://schemas.xmlsoap.org/wsdl/\" />\n";  
 74   
 75         if (empty($this->class_name)) {  
 76             throw new Exception(‘No class name.‘);  
 77         }  
 78           
 79         $class = new ReflectionClass($this->class_name);  
 80           
 81         if (!$class->isInstantiable()) {  
 82             throw new Exception(‘Class is not instantiable.‘);  
 83         }  
 84           
 85         $methods = $class->getMethods();  
 86           
 87         $portTypeWSDL = ‘<portType name="‘.$this->service_name.‘Port">‘;  
 88         $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";  
 89         $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‘]."/".$this->class_name.".php"."\" />\n</port>\n</service>\n";  
 90         $messageWSDL = ‘‘;  
 91         foreach ($methods as $method) {  
 92             if ($method->isPublic() && !$method->isConstructor()) {  
 93                 $portTypeWSDL.= ‘<operation name="‘.$method->getName()."\">\n".‘<input message="tns:‘.$method->getName()."Request\" />\n<output message=\"tns:".$method->getName()."Response\" />\n</operation>\n";  
 94                 $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";  
 95                 $messageWSDL.= ‘<message name="‘.$method->getName()."Request\">\n";  
 96                 $parameters = $method->getParameters();  
 97                 foreach ($parameters as $parameter) {  
 98                     $messageWSDL.= ‘<part name="‘.$parameter->getName()."\" type=\"xsd:string\" />\n";  
 99                 }  
100                 $messageWSDL.= "</message>\n";  
101                 $messageWSDL.= ‘<message name="‘.$method->getName()."Response\">\n";  
102                 $messageWSDL.= ‘<part name="‘.$method->getName()."\" type=\"xsd:string\" />\n";  
103                 $messageWSDL.= "</message>\n";  
104             }  
105         }  
106         $portTypeWSDL.= "</portType>\n";  
107         $bindingWSDL.= "</binding>\n";  
108         //return sprintf(‘%s%s%s%s%s%s‘, $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ‘</definitions>‘);  
109         $fso = fopen($this->class_name . ".wsdl" , "w");   
110         fwrite($fso, sprintf(‘%s%s%s%s%s%s‘, $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ‘</definitions>‘));   
111     }  
112       
113     /**  
114      * SoapDiscovery::getDiscovery() Returns discovery of WSDL.  
115      *   
116      * @return string  
117      **/  
118     public function getDiscovery() {  
119         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>";  
120     }  
121 }  
122   
123 ?> 

create_wsdl.php

1 <?php
2 
3 include("Service.php");
4 include("SoapDiscovery.class.php");
5 
6 $disco = new SoapDiscovery(‘Service‘, ‘my‘); //第一个参数是类名(生成的wsdl文件就是以它来命名的),即Service类,第二个参数是服务的名字(这个可以随便写)。
7 $disco->getWSDL();

client.php

 1 <?php
 2 header ("Content-Type: text/html; charset=utf-8");
 3 try {
 4     ini_set(‘soap.wsdl_cache_enabled‘, "0");    
 5     $client = new SoapClient(‘http://localhost/Service.php?wsdl‘);
 6     echo $client->echostr("hello");
 7     echo $client->Add(28, 222);
 8 } catch ( SoapFault $e ) {
 9     print "client.php:". $e->getMessage ();
10 }
11 
12 ?>

调用create_wsdl.php后生成wsdl文件,内容如下:注意下面红色地址

 1 <?xml version="1.0" ?>
 2 <definitions name="my" targetNamespace="urn:my" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="urn:my" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns="http://schemas.xmlsoap.org/wsdl/">
 3 <types xmlns="http://schemas.xmlsoap.org/wsdl/" />
 4 <portType name="myPort"><operation name="echostr">
 5 <input message="tns:echostrRequest" />
 6 <output message="tns:echostrResponse" />
 7 </operation>
 8 <operation name="Add">
 9 <input message="tns:AddRequest" />
10 <output message="tns:AddResponse" />
11 </operation>
12 </portType>
13 <binding name="myBinding" type="tns:myPort">
14 <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
15 <operation name="echostr">
16 <soap:operation soapAction="urn:my#Service#echostr" />
17 <input><soap:body use="encoded" namespace="urn:my" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
18 </input>
19 <output>
20 <soap:body use="encoded" namespace="urn:my" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
21 </output>
22 </operation>
23 <operation name="Add">
24 <soap:operation soapAction="urn:my#Service#Add" />
25 <input><soap:body use="encoded" namespace="urn:my" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
26 </input>
27 <output>
28 <soap:body use="encoded" namespace="urn:my" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
29 </output>
30 </operation>
31 </binding>
32 <service name="my">
33 <documentation />
34 <port name="myPort" binding="tns:myBinding"><soap:address location="http://localhost:80/Service.php" />
35 </port>
36 </service>
37 <message name="echostrRequest">
38 <part name="str" type="xsd:string" />
39 </message>
40 <message name="echostrResponse">
41 <part name="echostr" type="xsd:string" />
42 </message>
43 <message name="AddRequest">
44 <part name="a" type="xsd:string" />
45 <part name="b" type="xsd:string" />
46 </message>
47 <message name="AddResponse">
48 <part name="Add" type="xsd:string" />
49 </message>
50 </definitions>

访问client.php可以看到返回结果.

 

以上是关于php webservcie的主要内容,如果未能解决你的问题,请参考以下文章

java 调用webservcie ,自己亲测可用

java定时任务使用多线程webservcie执行了两次这是为啥?

WebServcie结合Spring结合动态代理进行抽象封装以及性能优化

超级有用的9个PHP代码片段

PHP必用代码片段

PHP代码-psysh调试代码片段工具