0

Been struggling all day trying to figure out a way to parse this xml with php:

$xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns1:recherchePointChronopostResponse xmlns:ns1="http://cxf.rechercheBt.soap.chronopost.fr/">
            <return>
                <errorCode>0</errorCode>
                <errorMessage>Code retour OK</errorMessage>
                <listePointRelais>
                    <accesPersonneMobiliteReduite>true</accesPersonneMobiliteReduite>
                    <actif>true</actif>
                    <adresse1>4 RUE DE MONTESSUY</adresse1>
                    <adresse2/>
                    <adresse3/>
                    <codePays>FR</codePays>
                    <codePostal>75007</codePostal>
                    <coordGeolocalisationLatitude>48.8597222222</coordGeolocalisationLatitude>
                    <coordGeolocalisationLongitude>2.304166666670</coordGeolocalisationLongitude>
                    <distanceEnMetre>852</distanceEnMetre>
                    <identifiant>2102R</identifiant>
                    <indiceDeLocalisation/>
                    <listeHoraireOuverture>
                        <horairesAsString>08:00-12:00 12:00-20:00</horairesAsString>
                        <jour>7</jour>
                        <listeHoraireOuverture>
                            <debut>08:00</debut>
                            <fin>12:00</fin>
                        </listeHoraireOuverture>
                        <listeHoraireOuverture>
                            <debut>12:00</debut>
                            <fin>20:00</fin>
                        </listeHoraireOuverture>
                    </listeHoraireOuverture>
                </listePointRelais>

                <qualiteReponse>2</qualiteReponse>
                <wsRequestId/>
            </return>
        </ns1:recherchePointChronopostResponse>
    </soap:Body>
</soap:Envelope>';

and here is the php i use:

$soap = simplexml_load_string($xml);
$soap->registerXPathNamespace('soap', 'http://cxf.rechercheBt.soap.chronopost.fr/');
foreach ($soap->xpath('//ns1:errorMessage') as $val) {
    echo $val . "<br>";
}

But i keep having a blank page!! do you please know what is wrong? I also tried to follow this post converting SOAP XML response to a PHP object or array, in vain!

Thanks

Community
  • 1
  • 1
  • Why not directly use PHP's SOAP client ? http://fr2.php.net/manual/en/class.soapclient.php – linkboss May 13 '14 at 16:15
  • Thanks for your reply I did try SoapClient() but didn't manage to make it work. found the following code and didn't undersrand well param1 and param2: $client = new SoapClient($xml); $res = $client->SoapFunction(array('param1'=>'value','param2'=>'value')); echo $res->PaymentNotification->payment; –  May 13 '14 at 20:41

1 Answers1

0

Give this a shot...

$xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:recherchePointChronopostResponse xmlns:ns1="http://cxf.rechercheBt.soap.chronopost.fr/">
    <return>
        <errorCode>0</errorCode>
        <errorMessage>Code retour OK</errorMessage>
        <listePointRelais>
            <accesPersonneMobiliteReduite>true</accesPersonneMobiliteReduite>
            <actif>true</actif>
            <adresse1>4 RUE DE MONTESSUY</adresse1>
            <adresse2/>
            <adresse3/>
            <codePays>FR</codePays>
            <codePostal>75007</codePostal>
            <coordGeolocalisationLatitude>48.8597222222</coordGeolocalisationLatitude>
            <coordGeolocalisationLongitude>2.304166666670</coordGeolocalisationLongitude>
            <distanceEnMetre>852</distanceEnMetre>
            <identifiant>2102R</identifiant>
            <indiceDeLocalisation/>
            <listeHoraireOuverture>
                <horairesAsString>08:00-12:00 12:00-20:00</horairesAsString>
                <jour>7</jour>
                <listeHoraireOuverture>
                    <debut>08:00</debut>
                    <fin>12:00</fin>
                </listeHoraireOuverture>
                <listeHoraireOuverture>
                    <debut>12:00</debut>
                    <fin>20:00</fin>
                </listeHoraireOuverture>
            </listeHoraireOuverture>
        </listePointRelais>

        <qualiteReponse>2</qualiteReponse>
        <wsRequestId/>
    </return>
</ns1:recherchePointChronopostResponse>
</soap:Body>
</soap:Envelope>';


$doc = new DOMDocument();
$doc->loadXML( $xml );

$results = $doc->getElementsByTagName( "errorMessage" );
$errorMessage = $results->item(0)->nodeValue;

echo $errorMessage;
Pedro
  • 451
  • 2
  • 5
  • Thank you for your message. Have just tried it, unfortunately it doesn't work :/ –  May 13 '14 at 20:45
  • What version of PHP are you running? – Pedro May 14 '14 at 20:55
  • I'm running under PHP5.3 –  May 15 '14 at 00:32
  • Okay it's not a PHP versioning issue since I just rolled back to 5.3 and 5.2 and both worked also. I'm not sure what to tell you. The code I pasted above for you works perfectly in my MAMP setup. Have you turned error reporting on? – Pedro May 15 '14 at 13:08
  • Yes, error reporting is on. don't you bother yourself. I have just figured out a way to do it: $xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/'); $xml->registerXPathNamespace('ns', 'http://cxf.rechercheBt.soap.chronopost.fr/'); foreach($xml->xpath('//soap:Envelope/soap:Body/ns:recherchePointChronopostResponse/return/listePointRelais') as $item) { echo (string) $item->adresse1.' - '.$item->listeHoraireOuverture->horairesAsString. '
    '; }
    –  May 15 '14 at 17:03