PHP请求webservice接口需要用到soap扩展,所以要先开启php.ini中的extension=php_soap.dll;
在实际项目应用中封装了一个方法进行调用,
public function requestSoap($url, $fun, $params)
{
$url = $url . '?wsdl';
$params = array(
'parameters' => $params
);
try {
libxml_disable_entity_loader(false);
$client = new \SoapClient($url);
$res = $client->__soapCall($fun, $params);
$funRe = $fun . 'Result';
$ret = json_decode($res->$funRe, true);
} catch (\Exception $e) {
$ret = $e->getMessage();
}
return $ret;
}
public function requestSoap($url, $fun, $params)
{
try {
libxml_disable_entity_loader(false);
$url = $url . '?wsdl';
$client = new \SoapClient($url);
$res = $client->$fun($params);
$funRe = $fun . 'Result';
$ret = json_decode($res->$funRe, true);
} catch (\Exception $e) {
$ret['msg'] = $e->getMessage();
}
return $ret;
}
注意:在调试过程中,会发现很多方法不起作用,提示不存在或不可用,是服务器端PHP的soap的wsdl缓存造成的!
两种方法解决:
1.清除soap缓存:打开php.ini,找到soap.wsdl_cache_dir="D:/wamp/tmp",这是soap的wsdl缓存的文件夹路径,删除该路径下的wsdl文件即可;
2.关闭saop缓存:打开php.ini,找到soap.wsdl_cache_enabled=1,将其改为soap.wsdl_cache_enabled=0,重启服务即可。