PHP调用.NET的asmx服务接口

由于业务需求对接.net服务(我这边是yii2框架),接口方要求登录后才能调用。调用过程中遇到了各种各样的错误,使用浏览器输入账号密码可以访问,但是使用SoapHeader类遇到了各种各样的报错。 网上关于这块的资料也比较少,最后下载了个SoapUI,根据这个工具的请求头一点一点试出来了。

在此说明两点:

  • .net提供的接口,请求时要在尾部加上"?wsdl"
  • 请求头最好按照请求的规则加上namespace、name,参数部分按照SoapUI中请求成功的结果全部填写上去,值也保持一致。

代码如下:

        header("content-type:text/html;charset=utf-8");
        //禁止soap缓存
        ini_set('soap.wsdl_cache_enabled', '0');
        //允许加载外部实体
        libxml_disable_entity_loader(false);
        $opts = array(
            'ssl' => array(
                'verify_peer' => false
            ),
            'https' => array(
                'curl_verify_ssl_peer' => false,
                'curl_verify_ssl_host' => false
            )
        );
        $streamContext = stream_context_create($opts);
        $client = new \SoapClient('http://X.X.X.X:9000/sd/sd.asmx?wsdl',
            array(
                'login' => 'user', 
                'password' => '123456', 
                'soap_version' => SOAP_1_1, 
                'encoding' => 'UTF-8',
                'stream_context' =>$streamContext 
            )
        );

        //此处参数模拟SoapUI请求成功的header头部分
        $param = array(
            'SourceId' => '?',
            'SourceName' => '?',
            'UserId' => '?',
            'SubmitDate' => '?',
            'SubmitTime' => '?',
            'UserName' => 'user',
            'Password'=>'123456');
        //'http://webservice.xxx.cn/SD'和'CredentialHeader'参照服务方给出的文档,或者在浏览器中访问接口地址查看
        $header = new \SoapHeader('http://webservice.xxx.cn/SD','CredentialHeader',$param,false);
        //添加soapheader
        $client->__setSoapHeaders($header);
        //GetInfo 为服务端提供的接口名称,后面是请求参数
        $response = $client->__soapCall("GetInfo", array(['curef' => '123', 'maincode' => '456']));

        var_dump($response);

你可能感兴趣的:(php,.net,soap,PHP,soap请求)