这里需要在网上下载一个nusoap类
服务器端代码:求和函数
<?php
require_once('Nusoap/nusoap.php');
$server = new soap_server();
$server->configureWSDL('My WebService'); //配置WSDL
//注册函数
$server->register('soap_add',//要调用的函数
//传入参数的数据类型
array('num1' => 'xsd:int', 'num2' => 'xsd:int'),
array('return' => 'xsd:int'));//返回值数据类型
function soap_add($num1, $num2)
{
//如果用户数据非法则抛出错误
if (trim($num1) != intval($num1) || trim($num2)
!= intval($num2))
{
return new soap_fault('1', '用户数据非法!');
}
else
{
return $num1 + $num2;
}
}
$server->service($HTTP_RAW_POST_DATA);
?>
客户端代码
<?
include('Nusoap/nusoap.php');
try
{
$client = new soapclient('
http://localhost/soapserver.php?wsdl', true);
$params = array('num1'=>10, 'num2'=>20);
$sum = $client->call('soap_add', $params);
echo "Summary: $sum";
}
catch(soap_fault $fault)
{
echo "错误:$fault->faultcode,
$fault->faultstring";
}
?>