0

I have the following request and am using guzzle to process it. I am attempting to get the ID and NAME hopefully in key => value array but it doesn't seem to work.

$api_url = 'http://example.com/test.asmx/GetUserDetails?userID=123';
$client = new Client();
$results = $client->request('GET', $api_url)->getBody()->getContents();
dd($results);

The response gotten when I echo $results is shown below

123EXAMPLE

Also, the response gotten when I dd($results) comes in triple quotes """ """ as shown below

"""
<?xml version="1.0" encoding="utf-8"?>

<DataSet xmlns="http://example.com">

  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ID" type="xs:string" minOccurs="0" />
                <xs:element name="NAME" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <NewDataSet xmlns="">
      <Table diffgr:id="Table1" msdata:rowOrder="0">
        <ID>123</ID>
        <NAME>EXAMPLE</NAME>
      </Table>
    </NewDataSet>
  </diffgr:diffgram>
</DataSet>
"""

I tried using SimpleXMLElement but it resulted in this response SimpleXMLElement {#642}

$response1 = simplexml_load_string($results);
$response2 = SimpleXMLElement($results);

Also, I used json_encode and json_decode but none worked.

Please, any help is appreciated.

ADyson
  • 57,178
  • 14
  • 51
  • 63
Ayomikun Samuel
  • 125
  • 2
  • 9
  • `Also, I used json_encode and json_decode`...why would you expect those to work with XML? That really makes no sense. – ADyson Oct 28 '21 at 21:18
  • `it resulted in this response SimpleXMLElement {#642}`... please update your question to show exactly how you used it. It sounds like maybe you just tried to echo the whole outer element without drilling down into it. See https://www.php.net/manual/en/simplexml.examples-basic.php for examples of basic usage. – ADyson Oct 28 '21 at 21:18
  • `The response gotten when I echo $results is shown below 123EXAMPLE`...I assume you mean you see this in a browser? That's because the browser sees the rest of the document as markup (like HTML - because HTML is a subset of XML) and so does not display it. All you're seeing there is the only bits of text in the XML document. – ADyson Oct 28 '21 at 21:20
  • Thanks for the update. All you've done there is simply load the document...if you echo those values directly then yes you'll just get a summary. What you need to do is try and access the elements within them, as per the examples in the link I gave you. – ADyson Oct 28 '21 at 22:04
  • thanks @ADyson still unable to access the data following the link you provided. I have tried `$response2->diffgr->NewDataSet->NewDataSet->Table->ID` but no response. I have also played with the format of getting the values but no result. – Ayomikun Samuel Oct 28 '21 at 22:49

1 Answers1

1

It seems the namespaces are complicating the situation. This code:

$response2 = new SimpleXMLElement($results);
$children = $response2->children('diffgr', true);
$children2 = $children->children();
echo $children2->NewDataSet->Table->ID;
echo PHP_EOL; //newline
echo $children2->NewDataSet->Table->NAME;

will get you the data.

It first gets all the children of the outer XML which have the "diffgr" namespace (which is only one, in this case). Then within that child, its own child elements don't have a namespace again, so we have to use the children() function again without a namepsace specified in order to retrieve those.

Live demo: http://sandbox.onlinephpfunctions.com/code/3cd49767e5b1dd237d26b9f03ceff4dae0546eae

Credit to this post for the idea.

ADyson
  • 57,178
  • 14
  • 51
  • 63