目前在PHP业界有两种利用方法:
一、利用soap扩展调用
二、引入lib/nusoap.php库,来操作

本日我重点来先容的是第一种利用办法,第二种已经掉队于时期了,不建议利用。
特殊把稳:
php里面还有nowsdl的调用办法,不建议利用,缘故原由:利用nowsdl过程创造只有php业务能正常通讯,其他措辞调用做事无法正常利用,不能跨措辞。
我本日给大家先容的是通过wsdl文件天生的soap做事,wsdl文件怎么天生可以利用zend工具。
详细来看代码:
command.php便是soap的做事端
client.php 是客户端
command.wsdl文件
command.php
ini_set("soap.wsdl_cache_enabled", "0");
Class WebService
{
private $returnSuccess = 'success';
private $returnFail = 'fail';
public function command($param = []){
return ['return' => $this->returnSuccess.json_encode($param)];
}
}
//这里可以用外部天生好的wsdl文件
$server = new \SoapServer('command.wsdl', ['uri' => "command.php", 'encoding' => 'utf-8']);
//设置做事类名
$server->setClass("WebService");
$server->handle();
client.php
header('Content-Type: text/xml; charset=UTF-8');
$client = new \SoapClient(
'http://127.0.0.1:8003/cmd-service/command.php?wsdl',
['encoding' => 'utf-8', 'cache_wsdl' => 0, 'compression' => true]
);
try {
$param =['enterpriseId' => 2,'randVal' => '111'];
//这里一定要加上parameters
$result = $client->__soapCall("command", ['parameters' => $param]);
} catch (Exception $e) {
echo $e->getMessage();exit;
}
echo $result->return;
command.wsdl文件
SoapDiscovery.class.php 下载这个类库去天生 或者 用zend天生require_once('command.php');define('WSDL_FILE','command.wsdl');if(!file_exists(WSDL_FILE)){ require_once('SoapDiscovery.class.php'); $sd = new SoapDiscovery('WebService', 'command'); $str = $sd->getWSDL(); file_put_contents(WSDL_FILE, $str);}