SOAP XML到PHP数组

问题描述 投票:0回答:2

当服务将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>
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;AssessmentResult xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns=&quot;http://ns.hr-xml.org/2007-04-15&quot;&gt;
  &lt;ReceiptId idOwner=&quot;Blank&quot;&gt;
    &lt;IdValue name=&quot;ReceiptID&quot;&gt;2461fg453f99-ea45dsg55-448464-85fgd80-e45fg77e5568b7f1&lt;/IdValue&gt;
    &lt;IdValue name=&quot;bID&quot;&gt;1422255467627&lt;/IdValue&gt;
  &lt;/ReceiptId&gt;
  &lt;ClientOrderId idOwner=&quot;BlankPartner&quot; /&gt;
  &lt;Results&gt;
    &lt;Profile&gt;Blank Credits&lt;/Profile&gt;
    &lt;SupportingMaterials&gt;
      &lt;Description&gt;No Forms Needed&lt;/Description&gt;
    &lt;/SupportingMaterials&gt;
    &lt;OverallResult&gt;
      &lt;Description&gt;Initial Eligibility&lt;/Description&gt;
      &lt;Score type=&quot;PotentialBlank1Eligibility&quot;&gt;0&lt;/Score&gt;
      &lt;Score type=&quot;PotentialBlank2Eligibility&quot;&gt;0&lt;/Score&gt;
    &lt;/OverallResult&gt;
    &lt;DetailResult&gt;
      &lt;Score type=&quot;Eligibility&quot;&gt;0&lt;/Score&gt;
    &lt;/DetailResult&gt;
  &lt;/Results&gt;
  &lt;AssessmentStatus&gt;
    &lt;Status&gt;Completed&lt;/Status&gt;
    &lt;Details&gt;No Errors&lt;/Details&gt;
    &lt;StatusDate&gt;2017-12-20T14:31:04.287072-05:00&lt;/StatusDate&gt;
  &lt;/AssessmentStatus&gt;
&lt;/AssessmentResult&gt;
</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数组。

php arrays xml soap
2个回答
1
投票

要访问内部数据元素......

$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处理,或将其转换为数组。第一部分提取肥皂:身体,然后它操纵它以获得最终的内容。


1
投票

例:

$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;
© www.soinside.com 2019 - 2024. All rights reserved.