php - 如何在 PHP 中将 XML 转换为数组
问题描述
我是 SOAP API 的新手,我得到了 Web 服务的响应。但我无法将其转换为数组以获取密钥。
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:setTransactionResponse xmlns:ns2="http://soap.api.controller.web.payjar.com/">
<return>
<merchantReference>mercRef_1395758213</merchantReference>
<payUReference>17613281409117</payUReference>
<successful>true</successful>
</return>
</ns2:setTransactionResponse>
</soap:Body>
</soap:Envelope>
解决方案
尝试以下对数组的 xml 响应
$xml = $response;
$xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $xml);
$xml = simplexml_load_string($xml);
$json = json_encode($xml);
$responseArray = json_decode($json,true);
echo "<pre>";print_r($responseArray);
输出
Array
(
[soapBody] => Array
(
[ns2setTransactionResponse] => Array
(
[return] => Array
(
[merchantReference] => mercRef_1395758213
[payUReference] => 17613281409117
[successful] => true
)
)
)
)
推荐阅读
- java - 从 6.1 升级到 7.5 后无法在 solr 中创建集合
- google-apps-script - Google Apps 脚本日期时间格式
- django - 使用带有 threading.Thead 的 django 频道
- jquery - 检查特定元素的代码是 XML Parser 中的最后一个子元素
- javascript - 无法将日历数据保存到 Firebase
- visual-c++ - 我应该如何更改我的编译器设置以使用带有外语的变量名
- android - BackHandler 'hardwareBackPress 只工作一次 | 反应原生
- time - Lua 以 UTC 检索时间无论如何都会返回本地时间
- c - 在 linux 中使用 ftok() 最多可以创建多少个共享内存密钥?
- mysql - Google App Engine MySQL 错误 1064:语法错误