我测试的环境为:apache2.3 php5.3
做这个测试之前,window要确认你的php配置文件中已经将soap扩展打开,即extension=php_soap.dll;
linux下要安装php_soap, 即yum install php_soap ;
server端
<?php
$classExample = array();
$soap = new SoapServer(null,array('uri'=>"localhost",'classExample'=>$classExample));
$soap->setClass('chesterClass');
$soap->handle();
class chesterClass {
function getList($args){
$ip = $_SERVER["REMOTE_ADDR"];
if ($ip!='192.168.1.1') {
return $_SERVER["REMOTE_ADDR"]."非法IP请求";
}
$arg=json_decode($args);
$num = (int)$arg->num;
$style=$arg->style;
$dept=$arg->dept;
$style=($style==1?'投诉':($style==2?'意见建议':($style==3?'咨询':'')));
if ($style=='') {
$where=($dept!='all'?" and rd_dept='{$dept}'":'');
}else{
$where=($dept!='all'?" and tz_dept='{$dept}'":'');
}
if ($style=='') {
$sql = "SELECT * from a";
}else {
$sql = "SELECT * from b";
}
$res = $this->getResult($sql);
$reslut = array();
$reslut2 = array();
while ($row=mysql_fetch_array($res,MYSQL_ASSOC)){
if($style!=''){
if($row['tz_state']=='回复'&& $row['tz_shenhe']=='审核') {
$row['tz_state']='已解决';
}else {
$row['tz_state']='解决中';
}
}
$reslut=$row;
array_push($reslut2, $reslut);
}
if(count($reslut2)>0) {
return json_encode($reslut2);
}else {
return "0";
}
}
function getResult($sql) {
$link=mysql_connect("localhost","root","");
mysql_select_db("sgjwms", $link); //选择数据库
mysql_query("SET names utf8");
$rs = mysql_query($sql, $link);
return $rs;
}
}
client端
<?php
//client端 clientSoap.php
try {
$client = new SoapClient(null,array('location' =>"http://192.168.100.80:5555/phpwebservice/server.php",'uri' => "http://127.0.0.1/")
);
$result=$client->getList('{"style":"3","num":"10","dept":"all"}');
print_r(json_decode($result));
} catch (SoapFault $fault){
echo "Error: ",$fault->faultcode,", string: ",$fault->faultstring;
}