simplexml_load with xmlns problem

Asked

Viewed 230 times

5

Well I have done everything I knew but is giving an error saying xmlns is not absolute. I have checked the xml and is correct now when

simplexml_load_string(): namespace Warning : xmlns: URI Cangooroo.Webservice.V2.Operator is not Absolute

    <?xml version="1.0" encoding="utf-8"?>
<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>
        <createClientResponse xmlns="http://v2.services.cangooroo.net/">
            <createClientResult>
                <ClientID xmlns="Cangooroo.Webservice.V2.Operator"></ClientID>
                <ClientCode xmlns="Cangooroo.Webservice.V2.Operator"></ClientCode>
                <CompleteName xmlns="Cangooroo.Webservice.V2.Operator">xxxx</CompleteName>
                <Address xmlns="Cangooroo.Webservice.V2.Operator">yyy</Address>
                <Phone1 xmlns="Cangooroo.Webservice.V2.Operator">(11)11111-111</Phone1>
                <Email xmlns="Cangooroo.Webservice.V2.Operator">[email protected]</Email>
                <NotificationEmail xmlns="Cangooroo.Webservice.V2.Operator">[email protected]</NotificationEmail>
                <PaymentType xmlns="Cangooroo.Webservice.V2.Operator"></PaymentType>
                <CanSeeVoucher xmlns="Cangooroo.Webservice.V2.Operator"></CanSeeVoucher>
                <CityName xmlns="Cangooroo.Webservice.V2.Operator"></CityName>
                <CountryName xmlns="Cangooroo.Webservice.V2.Operator"></CountryName>
                <BaseId xmlns="Cangooroo.Webservice.V2.Operator">0</BaseId>
                <BaseName xmlns="Cangooroo.Webservice.V2.Operator" />
                <ExternalReference xmlns="Cangooroo.Webservice.V2.Operator"></ExternalReference>
            </createClientResult>
        </createClientResponse>
    </soap:Body>
</soap:Envelope>

Well according to the documentation this would be one of the ways but this error can be repeated for the rest and their documentation is different from what I get in return....

$retorno = simplexml_load_string($xml_retorno);

In this way I accept suggestions.

1 answer

6


Before anything else I suggest you read the class documentation SoapClient and see if it is not the most suitable way for your project.


Case SoapClient Do not fit your project you can have in hand two serious problems and no resolution, but feasible. One contour is simple and another more complicated.

The first is the warning you came across:

simplexml_load_string(): namespace Warning : xmlns: URI Cangooroo.Webservice.V2.Operator is not Absolute

This warning occurs because the libxml identified the namespace Cangooroo.Webservice.V2.Operator as a relative URI. That’s the easy problem because to resolve just transform all references to URI relative Cangooroo.Webservice.V2.Operator in absolute URI, example: http://Cangooroo.Webservice.V2.Operator.

If you cannot modify the URI to circumvent this difficulty simply delete the alert report from libxml passing the parameter LIBXML_NOWARNING for the function simplexml_load_string()

Now comes the difficult problem. The function simplexml_load_string() cannot parse elements that are in a namespace other than the document root. That means you can read <soap:Envelope> and <soap:Body> but you won’t be able to read <createClientResponse> and their descendants.

This happens because when simplexml_load_string() is used to parse a document whose root is prefixed it is necessary to be informed in the parameter $ns prefix or namespace to be consulted. Any element outside the given namespace is discarded. What if the parameter $ns is not informed simplexml_load_string() cannot parse the document.

To circumvent you can remove the prefix soap of the document with str_ireplace().

<?php
$xmlstr = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<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>
        <createClientResponse xmlns="http://v2.services.cangooroo.net/">
            <createClientResult>
                <ClientID xmlns="Cangooroo.Webservice.V2.Operator"></ClientID>
                <ClientCode xmlns="Cangooroo.Webservice.V2.Operator"></ClientCode>
                <CompleteName xmlns="Cangooroo.Webservice.V2.Operator">xxxx</CompleteName>
                <Address xmlns="Cangooroo.Webservice.V2.Operator">yyy</Address>
                <Phone1 xmlns="Cangooroo.Webservice.V2.Operator">(11)11111-111</Phone1>
                <Email xmlns="Cangooroo.Webservice.V2.Operator">[email protected]</Email>
                <NotificationEmail xmlns="Cangooroo.Webservice.V2.Operator">[email protected]</NotificationEmail>
                <PaymentType xmlns="Cangooroo.Webservice.V2.Operator"></PaymentType>
                <CanSeeVoucher xmlns="Cangooroo.Webservice.V2.Operator"></CanSeeVoucher>
                <CityName xmlns="Cangooroo.Webservice.V2.Operator"></CityName>
                <CountryName xmlns="Cangooroo.Webservice.V2.Operator"></CountryName>
                <BaseId xmlns="Cangooroo.Webservice.V2.Operator">0</BaseId>
                <BaseName xmlns="Cangooroo.Webservice.V2.Operator" />
                <ExternalReference xmlns="Cangooroo.Webservice.V2.Operator"></ExternalReference>
            </createClientResult>
        </createClientResponse>
    </soap:Body>
</soap:Envelope>
XML;

$xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $xmlstr);

$doc = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOWARNING );

var_dump( $doc);


?>

Which results in:

object(SimpleXMLElement)#1 (1) {
  ["Body"]=>
  object(SimpleXMLElement)#2 (1) {
    ["createClientResponse"]=>
    object(SimpleXMLElement)#3 (1) {
      ["createClientResult"]=>
      object(SimpleXMLElement)#4 (14) {
        ["ClientID"]=>
        object(SimpleXMLElement)#5 (0) {
        }
        ["ClientCode"]=>
        object(SimpleXMLElement)#6 (0) {
        }
        ["CompleteName"]=>
        string(4) "xxxx"
        ["Address"]=>
        string(3) "yyy"
        ["Phone1"]=>
        string(13) "(11)11111-111"
        ["Email"]=>
        string(14) "[email protected]"
        ["NotificationEmail"]=>
        string(14) "[email protected]"
        ["PaymentType"]=>
        object(SimpleXMLElement)#7 (0) {
        }
        ["CanSeeVoucher"]=>
        object(SimpleXMLElement)#8 (0) {
        }
        ["CityName"]=>
        object(SimpleXMLElement)#9 (0) {
        }
        ["CountryName"]=>
        object(SimpleXMLElement)#10 (0) {
        }
        ["BaseId"]=>
        string(1) "0"
        ["BaseName"]=>
        object(SimpleXMLElement)#11 (0) {
        }
        ["ExternalReference"]=>
        object(SimpleXMLElement)#12 (0) {
        }
      }
    }
  }
}
  • 1

    opa! the Soap client I switched for a request in Curl and then it worked , really was a chaos kkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.