SOAP XML到PHP数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SOAP XML到PHP数组相关的知识,希望对你有一定的参考价值。
当服务将xml数据发布到我的网址时,我正在使用$data = file_get_contents('php://input');
。它给了我一个xml字符串,但我无法弄清楚如何把它变成一个php数组。我已经尝试了我可以在Stack上找到的每个解决方案,它总是给我一个空数组。这是xml:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<Authentication xmlns="urn:www.blank.com:blank:services:2:0:wsdl">
<username></username>
<password></password>
</Authentication>
</soap:Header>
<soap:Body>
<TransferDataString xmlns="urn:www.blank.com:blank:services:2:0:wsdl">
<data>
<?xml version="1.0"?>
<AssessmentResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ns.hr-xml.org/2007-04-15">
<ReceiptId idOwner="Blank">
<IdValue name="ReceiptID">2461fg453f99-ea45dsg55-448464-85fgd80-e45fg77e5568b7f1</IdValue>
<IdValue name="bID">1422255467627</IdValue>
</ReceiptId>
<ClientOrderId idOwner="BlankPartner" />
<Results>
<Profile>Blank Credits</Profile>
<SupportingMaterials>
<Description>No Forms Needed</Description>
</SupportingMaterials>
<OverallResult>
<Description>Initial Eligibility</Description>
<Score type="PotentialBlank1Eligibility">0</Score>
<Score type="PotentialBlank2Eligibility">0</Score>
</OverallResult>
<DetailResult>
<Score type="Eligibility">0</Score>
</DetailResult>
</Results>
<AssessmentStatus>
<Status>Completed</Status>
<Details>No Errors</Details>
<StatusDate>2017-12-20T14:31:04.287072-05:00</StatusDate>
</AssessmentStatus>
</AssessmentResult>
</data>
</TransferDataString>
</soap:Body>
</soap:Envelope>
为了默默无闻,我用“空白”代替了一些单词。
我已经尝试了递归的xml2array()
函数浮动(here, for instance)。返回空数组。
我试过了:$xml = simplexml_load_string($string, "SimpleXMLElement", LIBXML_NOCDATA);
$array = json_decode(json_encode((array)$xml), TRUE);
空数组。
我错过了什么?
UPDATE
我现在得到这个字符串:
<?xml version="1.0"?>
<AssessmentResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ns.hr-xml.org/2007-04-15">
<ReceiptId idOwner="Blank">
<IdValue name="ReceiptID">2461fg453f99-ea45dsg55-448464-85fgd80-e45fg77e5568b7f1</IdValue>
<IdValue name="bID">1422255467627</IdValue>
</ReceiptId>
<ClientOrderId idOwner="BlankPartner" />
<Results>
<Profile>Blank Credits</Profile>
<SupportingMaterials>
<Description>No Forms Needed</Description>
</SupportingMaterials>
<OverallResult>
<Description>Initial Eligibility</Description>
<Score type="PotentialBlank1Eligibility">0</Score>
<Score type="PotentialBlank2Eligibility">0</Score>
</OverallResult>
<DetailResult>
<Score type="Eligibility">0</Score>
</DetailResult>
</Results>
<AssessmentStatus>
<Status>Completed</Status>
<Details>No Errors</Details>
<StatusDate>2017-12-20T14:31:04.287072-05:00</StatusDate>
</AssessmentStatus>
</AssessmentResult>
从这个方法:
$response = preg_replace("/(</?)(w+):([^>]*>)/", "$1$2$3", $string);
$xml = new SimpleXMLElement($response);
$body = $xml->xpath('//soapBody')[0];
$array = json_decode(json_encode((array)$body), TRUE);
$data = $array['TransferDataString']['data'];
var_dump($data);
但我无法弄清楚如何将该字符串转换为PHP数组。
答案
要访问内部数据元素......
$xml = simplexml_load_string($data);
$xml->registerXPathNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
$data = $xml->xpath("//soap:Body");
$innerData = (string)$data[0]->TransferDataString->children("urn:www.blank.com:blank:services:2:0:wsdl")->data;
// Convert data to array
$xml2 = simplexml_load_string(trim($innerData));
$array = json_decode(json_encode($xml2), true);
print_r($array);
然后,您可以将数据作为XML处理,或将其转换为数组。第一部分提取肥皂:身体,然后它操纵它以获得最终的内容。
另一答案
例:
$dom = new DOMDocument("1.0", "UTF-8");
$dom->preserveWhiteSpace = false;
$dom->loadXml($source);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
$xpath->registerNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
$xpath->registerNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
$xpath->registerNamespace("xmlns", "urn:www.blank.com:blank:services:2:0:wsdl");
// Read the data element
$data = $xpath->query('//soap:Body/xmlns:TransferDataString/xmlns:data')->item(0)->nodeValue;
$data = trim($data);
echo $data;
以上是关于SOAP XML到PHP数组的主要内容,如果未能解决你的问题,请参考以下文章