欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

php通过soap扩展调用WebService接口 有大用

都2022年了。。有的接口方还在提供WebService接口。。居然不是http的接口。。!!这不。。最近要跟一个第三方进行接口对接,但是。。人家第三方的接口是用.NET写的WebService接口,c#调用通过,现在需要使用php来进行调用,折腾了好久,参考了各种资料!!经过各种探索,相关的PHP调用webservice的过程如下:

打开php相关扩展:

soap.so(windows系统对应的扩展后缀为:.dll)该扩展必须要开启

curl.so(windows系统对应的扩展后缀为:.dll)该扩展必须要开启

openssl.so(windows系统对应的扩展后缀为:.dll)视情况而开启,如果webservice接口服务端 是https,则该扩展需要开启

第三方接口信息(由于隐私设置,只展示第三方接口的部分参数信息)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
接口地址:http://192.168.148.149/MailWebServiceForZxy/ZfxxServiceForWebSite.asmx
 
1、accessCode参数:
公安:gongan
环保:huanbao
消防:xiaofang
应急:yingji
市场监管:shichangjianguan
城管:chengguan
  
2、inCodeInfo参数:test123
 
3、诉求提交接口
方法名称:SetAppealInterface(string accessCode,string inCodeInfo,string dataInfo)
 
入参:
string accessCode:加密的接入码
string inCodeInfo:校验码
string dataInfo:数据信息,格式如下
    <root><element des="信件信息">
    <attribute id="Title" des="标题">信件标题</attribute>
    <attribute id="username" des="网民用户名">网民用户名</attribute>
    <attribute id="address" des="地址">地址</attribute>
    <attribute id="email" des="邮箱">邮箱</attribute>
    <attribute id="mobile" des="手机">手机</attribute>
    </element>
    </root>
      
出参:
成功:唯一数据查询码(十位数字)
失败:0

php调用webserice接口代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//$client = new \SoapClient('http://192.168.148.149/MailWebServiceForZxy/ZfxxServiceForWebSite.asmx')
 
/*
注意:上面这样写会报:PHP Fatal error:  Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: 
Couldn't load from 'http://192.168.148.149/MailWebServiceForZxy/ZfxxServiceForWebSite.asmx' : 
EndTag: '</' not found
*/ 
 
正确写法是 在webservice地址后面加上?wsdl 即:下面这个正确写法:
$client new \SoapClient('http://192.168.148.149/MailWebServiceForZxy/ZfxxServiceForWebSite.asmx?wsdl');
 
//dataInfo参数比较多,这里我们单独使用一个变量来存储使用,注意:下面的xml串 不能有空格、换行
$datainfo_xml_str '<root><element des="信件信息"><attribute id="Title" des="标题">测试标题</attribute><attribute id="username" des="网民用户名">test</attribute><attribute id="userid" des="网民用户id">123456</attribute><attribute id="sex" des="性别">1</attribute><attribute id="address" des="地址">测试地址</attribute><attribute id="email" des="邮箱">test@qq.com</attribute><attribute id="mobile" des="手机">13954569999</attribute></element></root>';
 
//参数这样传递 先包装一下
$param array('accessCode' => 'huanbao''inCodeInfo' => 'test123''dataInfo' => $datainfo_xml_str);
 
//使用__soapCall()方法来进行调用
$p $client->__soapCall('SetAppealInterface'array('parameters' => $param)); //参数1:是webservice中的方法名  参数2:是 往方法里面传入的具体参数
 
print_r($p->SetAppealInterfaceResult); //输出:2203284111(对应上面接口信息中的成功出参(10位数字))  当然了,这里也可以先输出一下变量$p,看看输出结果是什么

注意,在php调用某个方法后,其soap对象 就会自动产生一个对应的Result方法 用来显示调用结果,如 上面的 被调用端的WebService中的"SetAppealInterface"方法,调用端就有相应的"SetAppealInterfaceResult"方法。

关于php的soap扩展的缓存问题:

php对soap的wsdl默认是开启缓存的,缓存时间默认1天,如果对方的webservices接口中的几个方法是用来查询的话,那对我们就不友好了。。。假如说 第一次调用对方的接口,对方webservices接口返回 空数据,如果对方接口在1天之内修改了返回数据,那我们再去调用的话,就会拿不到对方接口最新的数据,因为上一次调用接口返回的 空数据 被php的soap扩展给缓存了,解决办法就是关闭缓存,关闭方式如下:



代码中临时更改:

1
2
3
ini_set("soap.wsdl_cache", 0);
ini_set("soap.wsdl_cache_enabled", 0);
ini_set("soap.wsdl_cache_ttl", 0);


php.ini配置文件中进行修改

1
2
3
soap.wsdl_cache=1(改为0)
soap.wsdl_cache_enabled=1(改为0)
soap.wsdl_cache_ttl=86400(改为0)

改完php.ini后不要忘记重启php 使配置生效

尾声

关于php的soap扩展还有好多相关内置方法及使用方式,可以自行查看php官网或网上搜索使用资料 来解锁php soap扩展的更多使用姿势~!


当然了。。不仅是只能调用.NET写的webservice接口。。也可以调用java写的webservice接口,不区分语言,只要符合webservice规范,php都可以调用




声明:禁止任何非法用途使用,凡因违规使用而引起的任何法律纠纷,本站概不负责。


来自  https://www.css3er.com/p/337.html


普通分类: