PHP请求 webservice接口

1,312 阅读1分钟

PHP请求webservice接口需要用到soap扩展,所以要先开启php.ini中的extension=php_soap.dll;

在实际项目应用中封装了一个方法进行调用,

public function requestSoap($url, $fun, $params)
{
    $url = $url . '?wsdl';//接口地址
    $params = array(
        'parameters' => $params  //$param = array('username'=>'test','pwd'=>'111');
    );

    try {
        libxml_disable_entity_loader(false);
        $client = new \SoapClient($url);
        $res = $client->__soapCall($fun, $params);
        $funRe = $fun . 'Result';//在php调用某个方法后,其soap对象,就会自动产生一个Result方法,以方便显示调用结果
        $ret = json_decode($res->$funRe, true);
    } catch (\Exception $e) {
        $ret = $e->getMessage();
    }
    //var_dump($ret);
    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,重启服务即可。