0

I'm doing a SOAP call and get data returned in XML. The returning XML has a markup from which I don't know how to handle. I only need all <web_get_debiteuren>.

I thought of using php SimpleXMLElement (http://www.php.net/manual/en/simplexml.examples-basic.php). But I'm not able to do something like this:

$xml = new SimpleXMLElement($result);
echo $xml->soap;

How would I be able to wals through all results <web_get_debiteuren> part of the XML file?

See XML below:

<?xml version="1.0" encoding="windows-1250"?>
<soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:body>
    <getdatawithoptionsresponse xmlns="urn:Afas.Profit.Services">
      <getdatawithoptionsresult>
        <AfasGetConnector>
          <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:element name="AfasGetConnector">
              <xs:complexType>
                <xs:choice maxOccurs="unbounded">
                  <xs:element name="web_get_debiteuren">
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element name="Nummer_debiteur" type="xs:string" minOccurs="0"/>
                        <xs:element name="Naam_debiteur" type="xs:string" minOccurs="0"/>
                        <xs:element name="E-mail_werk" type="xs:string" minOccurs="0"/>
                        <xs:element name="Voornaam" type="xs:string" minOccurs="0"/>
                        <xs:element name="Achternaam" type="xs:string" minOccurs="0"/>
                      </xs:sequence>
                    </xs:complexType>
                  </xs:element>
                </xs:choice>
              </xs:complexType>
            </xs:element>
          </xs:schema>
          <web_get_debiteuren>
            <Nummer_debiteur>10000</Nummer_debiteur>
            <Naam_debiteur>Test</Naam_debiteur>
            <Voornaam>Hans</Voornaam>
            <Achternaam>Klok</Achternaam>
          </web_get_debiteuren>
          <web_get_debiteuren>
            <Nummer_debiteur>11000</Nummer_debiteur>
            <Naam_debiteur>Sven</Naam_debiteur>
            <E-mail_werk>e@mail.com</E-mail_werk>
            <Voornaam>Sven</Voornaam>
            <Achternaam>Kramer</Achternaam>
          </web_get_debiteuren>
          <web_get_debiteuren>
            <Nummer_debiteur>11001</Nummer_debiteur>
            <Naam_debiteur>Ireen</Naam_debiteur>
            <E-mail_werk>i@reen.nl</E-mail_werk>
            <Voornaam>Ireen</Voornaam>
            <Achternaam>Wust</Achternaam>
          </web_get_debiteuren>
        </AfasGetConnector>
      </getdatawithoptionsresult>
    </getdatawithoptionsresponse>
  </soap:body>
</soap:envelope>
Timo002
  • 3,138
  • 4
  • 40
  • 65
  • If you're using soap the return should be in object/array form. How are you getting raw xml? Through one of the debug function like __getLastResponse()? – Mike B Feb 17 '14 at 20:56
  • __getLastResponse() is empty. The SOAP service requires NTLM Authentication so the result is fetch by cURL. The XML that I've posted is the code that is returned from the SOAP call. – Timo002 Feb 17 '14 at 21:00

2 Answers2

0

You can use xpath. Samples with possible problems:

Using xpath on a PHP SimpleXML object, SOAP + namespaces (not working..) parse an XML with SimpleXML which has multiple namespaces

But I think, you should use SoapClient if this possible..

Community
  • 1
  • 1
Nostalgie
  • 554
  • 4
  • 5
  • SoapClient doesn't return anything. The SOAP service requires NTLM Authentication so the result is fetch by cURL. The XML that I've posted is the code that is returned from the SOAP call. Having a hard time to understand the xpath, but looking at it! – Timo002 Feb 17 '14 at 21:31
  • You may try this solution - http://www.php.net/manual/en/soapclient.soapclient.php#97029 – Nostalgie Feb 17 '14 at 21:43
  • I used this class, http://tcsoftware.net/blog/2011/08/php-soapclient-authentication/, which I thinks does the same! – Timo002 Feb 17 '14 at 21:44
0

Thanks to the tip of Nostalgie of using xpath, I got it working. See code below:

$response = simplexml_load_string( $result ,NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/" );

$response->registerXPathNamespace("site", "urn:Afas.Profit.Services");
$_res = $response->xpath('//site:AfasGetConnector');
#$_res = $_res->AfasGetConnector;

foreach($_res[0]->web_get_debiteuren AS $deb){
  echo "Number: ".$deb->Nummer_debiteur."<br>";
}

This will output:

Number: 10000
Number: 11000
Number: 11001
Number: 11002
Number: 11003
Number: 11004

Thanx!

Timo002
  • 3,138
  • 4
  • 40
  • 65