博主工作一周了,上一周里遇到了一个难题,就是对方提供的是SOAP协议制作出来的接口,让我们进行适配,其实这个技术已经淘汰了,不过对方是erp也不可能因为你而改写什么对吧,这里我们就分析一下怎么解决这个问题。

SOAP(Simple Object Access Protocol) 简单对象访问协议

SOAP是基于XML的建议协议,可使用应用程序的HTTP之上进行信息交换或简单地说:SOAP使用用于访问网络服务的协议。

  1. 什么是SOAP?
    • SOAP 指简易对象访问协议
    • SOAP 是一种通信协议
    • SOAP 用于应用程序之间的通信
    • SOAP 是一种发送消息的格式
    • SOAP 被设计用来通过因特网进行通信
    • SOAP 独立于平台
    • SOAP 独立于语言
    • SOAP 基于XML
    • SOAP 很简单并可扩展
    • SOAP 允许你绕过防火墙
    • SOAP 将被作为W3C标准来发展

2.
SOAP

6.约定的交互格式交互格式

1
2
3
4
5
6
7
8
9
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sen="http://pi.want-want.com/ZRFCARW_1/Sender_Syn">
<soapenv:Header/>
<soapenv:Body>
<sen:MT_ZRFCARW01_Req>
<!--Optional:-->
<IM_OBJID>{json数据}</IM_OBJID>
</sen:MT_ZRFCARW01_Req>
</soapenv:Body>
</soapenv:Envelope>

1
2
3
4
5
6
7
8
9
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sen="http://pi.want-want.com/ZRFCARW_2/Sender_Syn">
<soapenv:Header/>
<soapenv:Body>
<sen:MT_ZRFCARW02_Req>
<!--Optional:-->
<IM_JSON>{json数据}</IM_JSON>
</sen:MT_ZRFCARW02_Req>
</soapenv:Body>
</soapenv:Envelope>
  1. 鉴权方式
    – BaseAuthor验证方式

解决办法
  1. 通过对方提供wsdl文档文件,使用soapUI软件进行测试解析
  • soapUI工具图:

soapUI工具图

当我们使用这个工具跑通接口后,这个时候还没有完成我们还必须要使用一个工具就是postman这个接口测试工具,有人可能要问了,为啥用soapui这个工具呢?答案是:交互数据接口,我们需要读取wsdl文件啊,才能知道交互的数据结构呢。

  1. 使用postman软件进行接口的再次测试。
  • 从soapUI中获取相关信息进行解析使用postman进行测试

测试图1

  • 填入交互数据

交互数据图

  • 返回的结果图

返回结果图

我们获取到了正式的数据,我们还需要提取xml中的json数据,我得解决方案是通过正则匹配的形式提取json,还有另一种办法,因为数据的格式还是比较简单的,也可以采取替换的原则,去掉json的前后无用的xml标签数据


一下是我写的通信代码
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function ApiNetManager($json = null, $isID = true)
{
//根地址
$baseUrl = 'url';
$port = 'port';
$username = 'username';
$password = 'password';
//统一消息返回格式
$info = array(
'code' => 0,
'message' => '',
'data' => null
);
//验证json的正确性
if (is_null(json_decode($json))) {
return false;
}

//构建网络请求
$curl = curl_init();
if ($isID) {//验证课程ID
curl_setopt_array($curl, array(
CURLOPT_PORT => $port,
CURLOPT_URL => $baseUrl . ':' . $port . "/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_OEXAM&receiverParty=&receiverService=&interface=SI_ZRFCARW01_Out_Syn&interfaceNamespace=http%3A%2F%2Fpi.want-want.com%2FZRFCARW_1%2FSender_Syn",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "utf8",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:sen=\"http://pi.want-want.com/ZRFCARW_1/Sender_Syn\"><soapenv:Header/><soapenv:Body><sen:MT_ZRFCARW01_Req><IM_OBJID>{$json}</IM_OBJID></sen:MT_ZRFCARW01_Req></soapenv:Body></soapenv:Envelope>",
CURLOPT_HTTPHEADER => array(
"authorization: Basic " . base64_encode($username . ':' . $password),
"cache-control: no-cache",
"content-type: application/xml",
),
));

} else {//回传考试成绩接口
curl_setopt_array($curl, array(
CURLOPT_PORT => "50000",
CURLOPT_URL => $baseUrl . ':' . $port . "/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_OEXAM&receiverParty=&receiverService=&interface=SI_ZRFCARW02_Out_Syn&interfaceNamespace=http%3A%2F%2Fpi.want-want.com%2FZRFCARW_2%2FSender_Syn",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:sen=\"http://pi.want-want.com/ZRFCARW_2/Sender_Syn\"><soapenv:Header/><soapenv:Body><sen:MT_ZRFCARW02_Req><IM_JSON>{$json}</IM_JSON></sen:MT_ZRFCARW02_Req></soapenv:Body></soapenv:Envelope>",
CURLOPT_HTTPHEADER => array(
"authorization: Basic " . base64_encode($username . ':' . $password),
"cache-control: no-cache",
"content-type: application/xml",
),
));
}
//执行网络请求
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
$info['code'] = 500;
$info['message'] = '服务器异常,请稍后再试';
} else if (strstr($response, '<title>Error Report</title>')) {//请求成功权限没有通过
$info['code'] = 302;
$info['message'] = '服务器请求权限未通过请联系管理员!';
} else {//请求并权限通过
if ($isID) {//验证课程id的json查找
preg_match("/{.+}/", $response, $match);//查找json
} else {//考试成绩回传的json查找
preg_match('/\[{.*?\}]/is', $response, $match);
}
$jsonArr = json_decode($match[0], true);//解析json
$info['code'] = 200;
$info['message'] = '请求成功,成功返回数据';
$info['data'] = $jsonArr;
}
return $info['code'] == 0 ? false : $info;
}

至此我们完成了数据的代码的填写,相关具体测试我就不多说了,这里我加入了标准的接口返回形式,一边后续的代码可以更加精确地判断问题出在哪里,好了就到这里,谢谢大家的观看。