原文:http://www.cnblogs.com/wuhenke/archive/2010/09/30/1839424.html
原文:http://www.seayee.net/article/info_27.html
首先大家要简单了解了何谓webservice,接下来就做两个非常简单的例子,webservice还是逃不开server端与client端。
我测试的环境为:apache2.2.11 php5.2.10
做这个测试之前,要确认你的php配置文件中已经将soap扩展打开,即extension=php_soap.dll;
OK 现在我们来体验webservice
//server端 serverSoap.php
$soap = new SoapServer( null , array( 'uri' => "http://192.168.1.179/")); //This uri is your SERVER ip.
$soap -> addFunction( 'minus_func'); //Register the function
$soap -> addFunction( SOAP_FUNCTIONS_ALL);
$soap -> handle();
function minus_func( $i , $j ){
$res = $i - $j;
return $res;
}
//client端 clientSoap.php
try {
$client = new SoapClient( null ,
array( 'location' => "http://192.168.1.179/test/serverSoap.php" , 'uri' => "http://127.0.0.1/")
);
echo $client -> minus_func( 100 , 99);
} catch ( SoapFault $fault ){
echo "Error: " , $fault -> faultcode , ", string: " , $fault -> faultstring;
}
这是客户端调用服务器端函数的例子,我们再搞个class的。
//server端 serverSoap.php
$classExample = array();
$soap = new SoapServer( null , array( 'uri' => "http://192.168.1.179/" , 'classExample' => $classExample));
$soap -> setClass( 'chesterClass');
$soap -> handle();
class chesterClass {
public $name = 'Chester';
function getName() {
return $this -> name;
}
}
//client端 clientSoap.php
try {
$client = new SoapClient( null ,
array( 'location' => "http://192.168.1.179/test/serverSoap.php" , 'uri' => "http://127.0.0.1/")
);
echo $client -> getName();
} catch ( SoapFault $fault ){
echo "Error: " , $fault -> faultcode , ", string: " , $fault -> faultstring;
}
三、SOAP简单示例
SOAP开发一般有三种方式选择:
1)、PEAR自带的SOAP扩展;
2)、PHP自带的SOAP扩展;
3)、NuSOAP(纯PHP) 。
PHP 5中新增了内置的SOAP扩展,作为PHP的一部分提供的,因此不需要下载、安装和管理单独的包。这是第一个用C而不是PHP为PHP编写的SOAP实现,因此作者声称它的速度要快得多。相关文档包含在PHP手册的Function Reference部分(php_soap.dll)。
一个访问.NET WEB服务的客户端例子:
1
2
3
4
5
6
7
|
< ? php
$param
=
array
(
"ZipCode"
=>
'12209'
);
$out
=
$objSoapClient
->ValidateZip(
$param
);
$data
=
$out
->ValidateZipResult;
echo
$data
;
?>
|
四、实例
1)、用PHP建立SOAP服务
建立soap_server.php(虚拟路径为:http://localhost/php/soap/soap_server.php)
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
|
< ? php
/**
* A simple math utility class
*/
class
math{
/**
* Add two integers together
*
* @param integer $a The first integer of the addition
* @param integer $b The second integer of the addition
* @return integer The sum of the provided integers
*/
public
function
add(
$a
,
$b
){
return
$a
+
$b
;
}
/**
* Subtract two integers from each other
*
* @param integer $a The first integer of the subtraction
* @param integer $b The second integer of the subtraction
* @return integer The difference of the provided integers
*/
public
function
sub(
$a
,
$b
){
return
$a
-
$b
;
}
/**
* Div two integers from each other
*
* @param integer $a The first integer of the subtraction
* @param integer $b The second integer of the subtraction
* @return double The difference of the provided integers
*/
public
function
div(
$a
,
$b
){
if
(
$b
== 0){
throw
new
SoapFault(-1,
"Cannot divide by zero!"
);
}
return
$a
/
$b
;
}
}
$server
=
new
SoapServer(
'math.wsdl'
,
array
(
'soap_version'
=>SOAP_1_2));
$server
->setClass(
"math"
);
$server
->handle();
?>
|
注:
a)、math类是即将公开的webservice;
b)、$server->setClass,不是$server->addClass。
2)、用PHP客户端访问刚建立SOAP服务
1
2
3
4
5
6
7
8
9
10
|
< ? php
// $client = new SoapClient('http://localhost/php/soap/math.wsdl');
try
{
$result
=
$client
->div(8, 2);
// will cause a Soap Fault if divide by zero
print
"The answer is: $result"
;
}
catch
(SoapFault
$e
){
print
"Sorry an error was caught executing your request: {$e->getMessage()}"
;
}
?>
|
本质上,http://localhost/php/soap/soap_server.php?WSDL就是要访问到注释行所指的wsdl描述文件,所以这个WSDL文件必须事先生成。而对于其他语言如Java则可以动态生成。对于PHP自带的SOAP扩展要求这个WSDL文件必须事先生成好。
可以用ZendStudio生成静态的WSDL文件,此时用到math类的phpdoc作为生成WSDL的元数据。用ZendStudio生成wsdl文件时,必须正确说明Web服务目标地址,片断如下:
1
2
3
4
5
6
7
|
...
<service name=
"mathService"
>
<port binding=
"typens:mathBinding"
name=
"mathPort"
>
</port>
</service>
...
|
注:调用PHP Webserver的方法必须传入命名参数。