如何提取SOAP响应数据用于前端对比与输入框填充?
实现方案
一、PHP端:解析SOAP响应并完成数据匹配
SOAP响应是XML格式,需要先解析为PHP可操作的结构,再完成与用户输入的匹配,最后把处理结果以JSON格式返回给前端。
修改后的PHP代码
<?php // 获取用户提交的待匹配数据(假设通过POST传递) $userInput = $_POST['target_data'] ?? ''; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://www.dataaccess.com/webservicesserver/numberconversion.wso?WSDL=", CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://www.dataaccess.com/webservicesserver/\"> <soapenv:Header/> <soapenv:Body> <web:NumberToWords> <ubiNum>100</ubiNum> </web:NumberToWords> </soapenv:Body> </soapenv:Envelope>", CURLOPT_HTTPHEADER => array("content-type: text/xml"), )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); // 初始化返回结果 $output = [ 'match_success' => false, 'client_data' => [], 'error_msg' => '' ]; if ($err) { $output['error_msg'] = "cURL错误: " . $err; } else { // 解析XML响应 $xml = simplexml_load_string($response); if (!$xml) { $output['error_msg'] = "XML解析失败"; } else { // 注册响应中的命名空间(n0是响应里的前缀) $xml->registerXPathNamespace('n0', 'urn:sap-com:document:sap:rfc:functions'); // 提取客户数据节点 $dataNode = $xml->xpath('//n0:e_datos')[0]; // 转换为普通数组 $clientData = []; foreach ($dataNode->children() as $key => $value) { $clientData[(string)$key] = (string)$value; } // 执行数据匹配(示例:对比kunnrdf字段与用户输入) if (!empty($userInput) && $clientData['kunnrdf'] === $userInput) { $output['match_success'] = true; } $output['client_data'] = $clientData; } } // 输出JSON格式结果,供前端调用 header('Content-Type: application/json'); echo json_encode($output); ?>
注意:你提供的SOAP响应示例存在标签不闭合问题(如
<kunnrdf>123</kunnr>),实际接口返回的XML必须格式正确,否则解析会失败。
二、前端:数据提交、匹配结果展示与表单填充
通过AJAX请求PHP接口,获取处理结果后,先展示匹配状态,再自动填充表单输入框。
前端HTML结构
<form id="clientForm"> <div> <label>待验证编号:</label> <input type="text" id="target_data" name="target_data"> <button type="button" id="submitBtn">验证并填充</button> <span id="matchTip"></span> </div> <div> <label>客户名称:</label> <input type="text" id="name1"> </div> <div> <label>国家代码:</label> <input type="text" id="land1"> </div> <div> <label>街道:</label> <input type="text" id="stras"> </div> <div> <label>城市:</label> <input type="text" id="ort01"> </div> <div> <label>电话:</label> <input type="text" id="telf1"> </div> <!-- 其他需要填充的输入框依此类推 --> </form>
前端JS代码(原生JS实现)
document.getElementById('submitBtn').addEventListener('click', function() { const targetVal = document.getElementById('target_data').value.trim(); if (!targetVal) { alert('请输入待验证的编号'); return; } // 发送AJAX请求 const xhr = new XMLHttpRequest(); xhr.open('POST', '你的PHP文件路径.php', true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onload = function() { if (xhr.status === 200) { const res = JSON.parse(xhr.responseText); if (res.error_msg) { alert('请求出错:' + res.error_msg); return; } // 展示匹配结果 const tipEl = document.getElementById('matchTip'); if (res.match_success) { tipEl.textContent = '数据匹配成功'; tipEl.style.color = 'green'; } else { tipEl.textContent = '数据不匹配'; tipEl.style.color = 'red'; } // 自动填充表单 const data = res.client_data; for (const key in data) { const inputEl = document.getElementById(key); if (inputEl) { inputEl.value = data[key]; } } } else { alert('请求失败,请重试'); } }; xhr.send('target_data=' + encodeURIComponent(targetVal)); });
关键注意事项
- 命名空间处理:SOAP响应中的带前缀节点必须注册对应命名空间才能正确提取,否则XPath查询会失败。
- 输入框ID与数据键名对应:前端输入框的ID要和PHP返回的
client_data中的键名完全一致(如name1对应输入框IDname1),这样可以简化填充逻辑。 - 数据安全:处理用户输入时要做好转义,避免XSS攻击,
encodeURIComponent和json_encode已经帮你处理了大部分转义工作。
内容的提问来源于stack exchange,提问作者tyrese humphreys
相关产品推荐
相关产品推荐

