php webservice应用

大家要简单了解了何谓webservice,接下来就做两个非常简单的例子,webservice还是逃不开server端与client端。

我测试的环境为: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;
}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值