php使用soap(webservice)

1、定义服务端PersonInfo.php

<?php
Class PersonInfo{
public function getName(){
return “my name is zhangsan”;
}
}
?>

2、定义服务端server.php

<?php
include_once ‘PersonInfo.php';
$soap = new SoapServer(null,array(‘uri’=>”Server.php”));
$soap -> setClass(“PersonInfo”);
$soap->handle();
function minus_func($i, $j){
$res = $i – $j;
return $res;
}
?>

3、定义客户端client.php

<?php
try {
$client = new SoapClient(null,array(‘location’ =>”http://192.168.33.238/web/Server.php”,’uri’ =>”Server.php”));
echo $client->getName();

} catch (SoapFault $fault){
echo “Error: “,$fault->faultcode,”, string: “,$fault->faultstring;
}
?>

使用soap需要再php.ini中开启php_soap.dll扩展,定义好服务端的接口代码后就可以远程使用client调用接口代码了。