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可以看到返回结果.

时间: 2024-10-10 21:51:17

php webservcie的相关文章

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

webService抽象客户端封装.动态代理提升使用性能 1. 什么是webService webService就是在web上提供非相关系统与系统之间进行数据交互的一种服务.通过实现定义好的wsdl借口配置文件,进行约定以及调用. 在企业的内部系统中很常见,尤其是比较大型的企业,有数十种内部使用的系统,之间的通信基本上都是使用webService. 通俗点说就是:你要调用别人的服务,你就通过wsdl生成客户端代码,方便进行直接调用. 你需要被别人调用,你就通过wsdl生成服务端代码,方便对方系统

WebServcie【转】

WebService到底是什么? 一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用技术. Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布式的互操作的应用程序. Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实

java访问webservcie URL

1.  登录时保存cookie 1 private String responseCookie; 1 /** 2 * 根据URL地址和参数,获取返回数据 3 */ 4 private String login_postMethod(String url, String jsonParam) throws IOException { 5 URL postUrl = new URL(url); 6 HttpURLConnection connection = (HttpURLConnection)

java 调用webservcie ,自己亲测可用

第一次用 java 调webervice,网上看了很多博客,能用的没几个,综合几个博客,终于成功调用了. package com.casco.action;import org.apache.axis.client.Call; import org.apache.axis.client.Service; import javax.xml.namespace.QName; public class syncProjectData { public void syncProject(String p

自定义及发布一个webservice服务

自定义及发布一个webservice服务    - 声明 某个业务服务为webservice服务       通过@webservice 注解来声明    - 发布webservice服务       Endpoint.publish()发布 (默认对public修饰的方法进行发布)    - 通过wsimport生成本地代理来访问自己发布的webservice       wsimport 1.发布自定义webservice phone.java package ws.myWebService

java webservice

1.1 [什么是webservice] 1.1.1 什么是webservice? l Web service 即web服务,它是一种跨编程语言和跨操作系统平台的远程调用技术即跨平台远程调用技术. l 采用标准SOAP(Simple Object Access Protocol)  协议传输,soap属于w3c标准.Soap协议是基于http的应用层协议,soap协议传输是xml数据. l 采用wsdl作为描述语言即webservice使用说明书,wsdl属w3c标准. l xml是webserv

BOS物流管理系统-第五天

BOS物流管理系统-第五天-定区管理-WebServcie远程调用 主要内容: 分区设置-导出(分区条件查询后的结果导出为Excel-POI生成Excel和文件下载) 定区管理---定区添加(定区关联分区和取派员,easyUi相关的注意的地方) 定区管理-分页条件查询(复习-form表单json转换,Spring Data Specification ) 定区管理-定区关联客户(模拟系统间:bos和crm(Customer Relational Managerment)的远程调用-WebServ

03_wsdl和soap

03_wsdl和soap讲解(介入了tcpmon工具) WSDL内容: 1.types:用来定义访问的类型 2.message:SOAP(simple object access Protocol) 3.portType:指明服务器的接口,并且通过operation绑定相应的in和out的消息:其中in表示参数,out表示返回值 4.binding:指定传递消息所使用的格式:literal 5.service:指定服务所发布的名称 使用tcpmon工具查看webService TCPMon是ap

02_wsimport的使用

java使用命令: D:\>wsimport -d d:/work_space/webservice_spqce/01/ -keep -verbose http://localhost:8888/ws?wsdl “ -d d:/work_space/webservice_spqce/01/”:指定生成的目录 " -keep":指定是否生成.java文件 “-verbose”:显示生成的详细过程 “http://localhost:8888/ws?wsdl”:网络中的wsdl文件