您的位置:首页 > 编程语言 > PHP开发

用PHP写和调用webservice最简单方法

2016-02-26 14:36 696 查看
服务端代码  文件名mainservice.php

<?php

//其中uri相当于java中的命名空间,可以是任何不和别人重合的字符串,第一个参数是wsdl,如果不使用wsdl则使用null,第三个参数表示soap的版本号,目前就两个版本号

$wbServer= new SoapServer(null, array('uri'=>'textphpwebservice','soap_version'=>SOAP_1_2));

$wbServer->addFunction('sayhello');

$wbServer->addFunction('mymin');

$wbServer->handle();

function sayhello($name){

return "hello ".$name."you
are great!";

}

function mymin($a,$b){

return $a>$b?$b:$a;

}

exit();

?>

客户端代码

<?php

try{

//第一个参数是wsdl文件,不使用的话用null,location表示webservice服务文件的地址,uri要用要调用的webserivce中的uri一致

$client=new SoapClient(null,array('location'=>'http://localhost/mainservice.php','uri'=>'textphpwebservice'));

echo $client->sayhello("lijh")."<br
/>";

echo $client->mymin(100,50)."ccccc";

}catch(SoapFault $fault){

echo "fail";

}

exit();

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: