文章来源:http://www.cnblogs.com/likwo/archive/2009/09/18/1569036.html
在网上找了很多制作PHP的Web Service的例子,但是都不太全面。以下是我的php制作过程。
1. 首先制作提供WebService的php页面。
//
myservice.php
<? php
class service
{
public function HelloWorld()
{
return " Hello " ;
}
public function Add( $a , $b )
{
return $a + $b ;
}
}
// TestSoap.wsd需要在后面采用Zend Studio生成。
$server = new SoapServer( ' TestSoap.wsdl ' , array ( ' soap_version ' => SOAP_1_2));
$server -> setClass( " service " );
$server -> handle();
?>
<? php
class service
{
public function HelloWorld()
{
return " Hello " ;
}
public function Add( $a , $b )
{
return $a + $b ;
}
}
// TestSoap.wsd需要在后面采用Zend Studio生成。
$server = new SoapServer( ' TestSoap.wsdl ' , array ( ' soap_version ' => SOAP_1_2));
$server -> setClass( " service " );
$server -> handle();
?>
2.生成WSDL文件。采用Zend Studio生成。
生成过程:选择菜单File-->Export-->PHP->WSDL FILE
也就是出现下面的图
选择 next ,出现如下WSDL选择画面
File Name:选择需要生成的WSDL文件存储文件名和路径
Exported file 选择学要前面写的myservice.php文件,可以添加多个。
Classes:选择需要制作WebService的文件,注意:URL:一定要写上myservice.php的 URL.如果提供服务的是其他php页面,那就换成其他的php服务页面。
Global Settings:如下图所示:
选择Finish,生成WSDL文件,下面是Zend Studio里的效果图
3.客户端调用该Web Service
//
Client.php
<? php
$a = new SoapClient( " TestSoap.wsdl " );
echo $a -> HelloWorld();
echo ( " <br /> " );
echo $a -> Add( 1 , 2 );
?>
<? php
$a = new SoapClient( " TestSoap.wsdl " );
echo $a -> HelloWorld();
echo ( " <br /> " );
echo $a -> Add( 1 , 2 );
?>
到此为止,一个简单的Php WebService制作完成。
扩展:如何用WebService实现数据库的GRUD操作,后续发布。
补充:
TestSoap.wsdl文件到底是什么
<?
xml version
=
'
1.0
'
encoding
=
'
UTF-8
'
?>
<!-- WSDL file generated by Zend Studio . -->
< definitions name = " TestSoap " targetNamespace = " urn:TestSoap " xmlns : typens = " urn:TestSoap " xmlns : xsd = " http://www.w3.org/2001/XMLSchema " xmlns : soap = " http://schemas.xmlsoap.org/wsdl/soap/ " xmlns : soapenc = " http://schemas.xmlsoap.org/soap/encoding/ " xmlns : wsdl = " http://schemas.xmlsoap.org/wsdl/ " xmlns = " http://schemas.xmlsoap.org/wsdl/ " >
< message name = " Add " >
< part name = " a " />
< part name = " b " />
</ message >
< message name = " AddResponse " >
< part name = " AddReturn " />
</ message >
< message name = " HelloWorld " />
< message name = " HelloWorldResponse " >
< part name = " HelloWorldReturn " />
</ message >
< message name = " Sub " >
< part name = " a " />
</ message >
< message name = " SubResponse " >
< part name = " SubReturn " />
</ message >
< portType name = " servicePortType " >
< operation name = " Add " >
< input message = " typens:Add " />
< output message = " typens:AddResponse " />
</ operation >
< operation name = " HelloWorld " >
< input message = " typens:HelloWorld " />
< output message = " typens:HelloWorldResponse " />
</ operation >
< operation name = " Sub " >
< input message = " typens:Sub " />
< output message = " typens:SubResponse " />
</ operation >
</ portType >
< binding name = " serviceBinding " type = " typens:servicePortType " >
< soap : binding style = " rpc " transport = " http://schemas.xmlsoap.org/soap/http " />
< operation name = " Add " >
< soap : operation soapAction = " urn:serviceAction " />
< input >
< soap : body namespace = " urn:TestSoap " use = " literal " />
</ input >
< output >
< soap : body namespace = " urn:TestSoap " use = " literal " />
</ output >
</ operation >
< operation name = " HelloWorld " >
< soap : operation soapAction = " urn:serviceAction " />
< input >
< soap : body namespace = " urn:TestSoap " use = " literal " />
</ input >
< output >
< soap : body namespace = " urn:TestSoap " use = " literal " />
</ output >
</ operation >
< operation name = " Sub " >
< soap : operation soapAction = " urn:serviceAction " />
< input >
< soap : body namespace = " urn:TestSoap " use = " literal " />
</ input >
< output >
< soap : body namespace = " urn:TestSoap " use = " literal " />
</ output >
</ operation >
</ binding >
< service name = " TestSoapService " >
< port name = " servicePort " binding = " typens:serviceBinding " >
< soap : address location = " http://localhost/test/soap/myservice.php " />
</ port >
</ service >
</ definitions >
但是我发觉他和.net 产生的WSDL不一样。而且通过这种方式有一个弊端,如果我的Service里添加了一个新的方法,需要在重新生成WSDL文件,
<!-- WSDL file generated by Zend Studio . -->
< definitions name = " TestSoap " targetNamespace = " urn:TestSoap " xmlns : typens = " urn:TestSoap " xmlns : xsd = " http://www.w3.org/2001/XMLSchema " xmlns : soap = " http://schemas.xmlsoap.org/wsdl/soap/ " xmlns : soapenc = " http://schemas.xmlsoap.org/soap/encoding/ " xmlns : wsdl = " http://schemas.xmlsoap.org/wsdl/ " xmlns = " http://schemas.xmlsoap.org/wsdl/ " >
< message name = " Add " >
< part name = " a " />
< part name = " b " />
</ message >
< message name = " AddResponse " >
< part name = " AddReturn " />
</ message >
< message name = " HelloWorld " />
< message name = " HelloWorldResponse " >
< part name = " HelloWorldReturn " />
</ message >
< message name = " Sub " >
< part name = " a " />
</ message >
< message name = " SubResponse " >
< part name = " SubReturn " />
</ message >
< portType name = " servicePortType " >
< operation name = " Add " >
< input message = " typens:Add " />
< output message = " typens:AddResponse " />
</ operation >
< operation name = " HelloWorld " >
< input message = " typens:HelloWorld " />
< output message = " typens:HelloWorldResponse " />
</ operation >
< operation name = " Sub " >
< input message = " typens:Sub " />
< output message = " typens:SubResponse " />
</ operation >
</ portType >
< binding name = " serviceBinding " type = " typens:servicePortType " >
< soap : binding style = " rpc " transport = " http://schemas.xmlsoap.org/soap/http " />
< operation name = " Add " >
< soap : operation soapAction = " urn:serviceAction " />
< input >
< soap : body namespace = " urn:TestSoap " use = " literal " />
</ input >
< output >
< soap : body namespace = " urn:TestSoap " use = " literal " />
</ output >
</ operation >
< operation name = " HelloWorld " >
< soap : operation soapAction = " urn:serviceAction " />
< input >
< soap : body namespace = " urn:TestSoap " use = " literal " />
</ input >
< output >
< soap : body namespace = " urn:TestSoap " use = " literal " />
</ output >
</ operation >
< operation name = " Sub " >
< soap : operation soapAction = " urn:serviceAction " />
< input >
< soap : body namespace = " urn:TestSoap " use = " literal " />
</ input >
< output >
< soap : body namespace = " urn:TestSoap " use = " literal " />
</ output >
</ operation >
</ binding >
< service name = " TestSoapService " >
< port name = " servicePort " binding = " typens:serviceBinding " >
< soap : address location = " http://localhost/test/soap/myservice.php " />
</ port >
</ service >
</ definitions >
而且还经常出错误。但是如果采用Zend WSDL编辑器产生的WSDL感觉更标准写,而且他相对于目前这样的方法不需要删除WSDL重新建,只需要编辑目前已有的。