0

I'm using $data = file_get_contents('php://input'); when a service is posting xml data to my url. It's giving me an xml string, but I can't figure out how to turn it into a php array. I've tried every solution I can find here on Stack, and it always gives me an empty array. Here's the 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>

I've replaced some words with "blank" for obscurity.

I've tried the recursive xml2array() functions floating about (here, for instance). Returns empty array.

I've tried: $xml = simplexml_load_string($string, "SimpleXMLElement", LIBXML_NOCDATA); $array = json_decode(json_encode((array)$xml), TRUE);

Empty array.

What am I missing?

UPDATE

I am now getting this string:

<?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>

from this method:

$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);

But I cannot figure out how to convert that string to a PHP array.

2
  • Do you try to read the data element or the whole xml document? Commented Dec 20, 2017 at 21:08
  • See update in OP. Commented Dec 20, 2017 at 21:42

2 Answers 2

1

To access the inner data elements...

$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);

You can then process the data either as XML, or convert it to an array. The first part extracts the soap:Body and then it manipulates that to get at the final inner content.

Sign up to request clarification or add additional context in comments.

3 Comments

How do I convert the data to an array?
Updated with conversion.
Thanks! It was performing the trim() that I was missing.
1

Example:

$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;

1 Comment

How do I convert the data to an array? That's what I am asking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.