首先环境是:php5.3以上+mysql+apache
讲解前,先配置下PHP的soap环境支持:
找到php.ini文件
;extension=php_soap.dll
删除掉";" ,重启apache服务器
第一步就需要使用类 "SoapDiscovery.class.php" 生成wsdl文档!
具体怎生成wsdl文件的,就算了,还是讲讲怎么操作的,直接使用SoapDiscovery.class.php能创建就行。哈哈哈
很简单,这里上段代码,利用类文件创建wsdl接口文件。运行文件就能得到。
define('WSDL_URL','hello.wsdl'); //定义WSDL文件路径
ini_set('soap.wsdl_cache_enabled','0'); //关闭WSDL缓存
//WSDL文件不存在时自动创建
if(!file_exists(WSDL_URL))
{
require_once 'SoapDiscovery.class.php';
$disco = new SoapDiscovery('Mywsdl','www.zhuoqu.com');
$str = $disco->getWSDL();
file_put_contents(WSDL_URL,$str);
}
//SOAP开启并接收Client传入的参数响应
$server = new SoapServer(WSDL_URL);
$server->setClass('Mywsdl');
$server->handle();
//测试定义公开的类
class Mywsdl {
private $nombre = '';
public function __construct($name = 'World')
{
$this->name = $name;
}
public function greet($name = '')
{
$name = $name?$name:$this->name;
return 'Hello '.$name.'.';
}
public function serverTimestamp()
{
return time();
}
public function myfunc($a=''){
return $a;
}
然后就是访问而已$client = new SoapClient("http://127.0.0.1/swdl/hello.wsdl");
try {
$result = $client->myfunc('789');
var_dump($result);
//echo "The answer isresult";
}
catch (SoapFault $f){
echo "Error Message: {$f->getMessage()}";
}
?>
就这样,上次源码,可测试。