
本文探讨了将复杂php多维数组转换为符合soap协议要求的xml结构,特别是涉及深层嵌套、自定义属性(如`xsi:type`)和重复元素时的挑战。文章指出传统domdocument递归方法在此类场景下的局限性,并详细介绍了如何利用`spatie/array-to-xml`库,通过构建特定结构的php数组,高效、准确地生成符合soap规范的xml输出,从而解决序列化难题。
在现代Web服务交互中,将后端数据结构(如PHP数组)序列化为XML格式是常见的需求,尤其是在与基于SOAP协议的Web服务进行通信时。SOAP XML通常要求精确的元素名称、命名空间、数据类型属性(如xsi:type)以及复杂的嵌套结构,这使得简单的递归转换方法往往力不从心。
SOAP协议通过XML来封装消息,其结构通常由SOAP-ENV:Envelope、SOAP-ENV:Header(可选)和SOAP-ENV:Body组成。在SOAP-ENV:Body内部,会包含具体的业务数据。一个典型的SOAP XML响应可能如下所示,其中包含多层嵌套、自定义命名空间以及关键的xsi:type属性来指明数据类型:
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://ex.pt/soap/WebServices">
<SOAP-ENV:Body>
<ns1:Person xmlns:ns1="https://ex.pt/webservices">
<data xsi:type="tns:getPersonInfo">
<name xsi:type="xsd:string">John</name>
<surname xsi:type="xsd:string">Doe</surname>
<job xsi:type="xsd:string">developer</job>
<from xsi:type="xsd:string">france</from>
<address xsi:type="tns:getAddress">
<country xsi:type="xsd:string">france</country>
<city xsi:type="xsd:string">paris</city>
<post_code xsi:type="xsd:string">12345</post_code>
</address>
<items xsi:type="tns:getItems">
<item>
<name xsi:type="xsd:string">spoon</name>
<material xsi:type="xsd:string">vacuum</material>
</item>
</items>
</data>
</ns1:Person>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>上述XML结构展示了几个关键点:
当尝试使用PHP内置的DOMDocument类进行递归转换时,自定义一个函数来处理上述复杂性会变得非常繁琐。例如,原始问题中提供的convertToXml函数在处理多维数组时,可能无法灵活地:
立即学习“PHP免费学习笔记(深入)”;
这些挑战导致在处理复杂SOAP XML结构时,自定义的DOMDocument方法容易出错,且难以维护。
为了更高效、准确地将复杂PHP数组转换为SOAP XML,推荐使用像spatie/array-to-xml这样的专业库。该库提供了一种声明式的方式,通过构建一个特定结构的PHP数组来映射到所需的XML输出,极大地简化了开发过程。
首先,通过Composer安装spatie/array-to-xml库:
composer require spatie/array-to-xml
spatie/array-to-xml库通过识别数组中的特殊键来处理XML元素的属性、文本内容以及重复元素。
基于SOAP XML的预期结构,我们需要将原始PHP数组转换为spatie/array-to-xml能够理解的格式。以下是根据预期SOAP XML和库特性构建的PHP数组示例:
<?php
use Spatie\ArrayToXml\ArrayToXml;
// 原始PHP数组(作为参考,实际转换时需要重构)
$originalData = [
"name" => "John Doe",
"date" => "2021-11-30 00:00:00.000",
"job" => "developer",
"where_from" => "france",
"address" => [
"country" => "france",
"city" => "paris",
"vat_number" => "123456"
],
"items" => [
[
"cook" => "spoon",
"clean" => "vacuum"
]
]
];
// 构建符合 spatie/array-to-xml 要求的数组结构
$dataForXml = [
'SOAP-ENV:Body' => [
'ns1:Person' => [
'data' => [ // 注意这里将info改名为data以匹配期望XML
"name" => ['_attributes' => ['xsi:type' => 'xsd:string'], '_value' => "John Doe"],
"surname" => ['_attributes' => ['xsi:type' => 'xsd:string'], '_value' => "Doe"], // 假设原始数据中包含姓氏
"job" => ['_attributes' => ['xsi:type' => 'xsd:string'], '_value' => "developer"],
"from" => ['_attributes' => ['xsi:type' => 'xsd:string'], '_value' => "france"],
"address" => [
"country" => ['_attributes' => ['xsi:type' => 'xsd:string'], '_value' => "france"],
"city" => ['_attributes' => ['xsi:type' => 'xsd:string'], '_value' => "paris"],
"post_code" => ['_attributes' => ['xsi:type' => 'xsd:string'], '_value' => "123456"], // 假设原始数据中vat_number对应post_code
'_attributes' => ['xsi:type' => 'tns:getAddress']
],
"items" => [
// 处理多个 item 元素,使用 __custom:elementName:index 模式
'__custom:item:0' => [ // 对应原始数组 items[0]
"name" => ['_attributes' => ['xsi:type' => 'xsd:string'], '_value' => "spoon"], // 对应原始数组 cook
"material" => ['_attributes' => ['xsi:type' => 'xsd:string'], '_value' => "vacuum"], // 对应原始数组 clean
],
// 如果有更多 item,可以继续添加 '__custom:item:1' => [...]
'_attributes' => ['xsi:type' => 'tns:getItems']
],
'_attributes' => ['xsi:type' => 'tns:getPersonInfo']
],
'_attributes' => ['xmlns:ns1' => 'https://ex.pt/webservices']
]
]
];
// 使用 ArrayToXml::convert 方法进行转换
$response = ArrayToXml::convert($dataForXml, [
'rootElementName' => 'SOAP-ENV:Envelope',
'_attributes' => [
'SOAP-ENV:encodingStyle' => 'http://schemas.xmlsoap.org/soap/encoding/',
'xmlns:SOAP-ENV' => 'http://schemas.xmlsoap.org/soap/envelope/',
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns:SOAP-ENC' => 'http://schemas.xmlsoap.org/soap/encoding/',
'xmlns:tns' => 'http://ex.pt/soap/WebServices'
],
], true, 'UTF-8');
echo $response;在上述代码中:
最后,调用ArrayToXml::convert()方法,传入构建好的数组、根元素配置以及全局属性,即可生成完整的SOAP XML字符串。rootElementName参数定义了最外层的XML元素,_attributes则为该根元素添加了所有必要的命名空间和编码样式属性。
将复杂的PHP多维数组转换为符合SOAP协议要求的XML结构是一项常见的任务,但由于SOAP XML的严格性和复杂性,传统的手动DOMDocument递归方法往往难以胜任。通过利用spatie/array-to-xml这样的专业库,并遵循其特定的数组结构映射规则,开发者可以高效、准确地生成所需的XML输出。这种方法不仅简化了代码,提高了可维护性,也更好地适应了SOAP Web服务对XML格式的严格要求。
以上就是PHP多维数组到SOAP XML的序列化实践:处理复杂结构与属性的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号