Sascha
Sascha

Reputation: 3

Read soap xml with php

Anyone can help on this how I can get the value of callerId from this string?
String is pulled from php://input and i need this with just php without soap class and only this one value no loop.
Any idea?

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:authorizePlayer xmlns:ns2="http://authorization.wallet.de">
            <authorizationRequest>
                <callerId>mycallerid</callerId>
                <callerPassword>mypassword</callerPassword>
                <playerName>player</playerName>
                <sessionToken>b0f4617bb56f1ed4706908b6ab9a4960</sessionToken>
            </authorizationRequest>
        </ns2:authorizePlayer>
    </S:Body>
</S:Envelope>

Upvotes: 0

Views: 731

Answers (2)

fusion3k
fusion3k

Reputation: 11689

You can easily use DOMDocument.

Assuming your string is in $xml variable, try this code:

$dom = new DOMDocument();
$dom->loadXML( $xml, LIBXML_NOBLANKS );
$callerId = $dom->getElementsbyTagName( 'callerId' )->item(0)->nodeValue;

3v4l.org demo


Upvotes: 1

The fourth bird
The fourth bird

Reputation: 163632

Another option is to use the SimpleXMLElement's xpath method and register the namespace.

$element is of type SimpleXMLElement and you can call its __toString() method to return the string content:

$source = <<<SOURCE
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:authorizePlayer xmlns:ns2="http://authorization.wallet.de">
            <authorizationRequest>
                <callerId>mycallerid</callerId>
                <callerPassword>mypassword</callerPassword>
                <playerName>player</playerName>
                <sessionToken>b0f4617bb56f1ed4706908b6ab9a4960</sessionToken>
            </authorizationRequest>
        </ns2:authorizePlayer>
    </S:Body>
</S:Envelope>
SOURCE;

$xml = simplexml_load_string($source);
$xml->registerXPathNamespace('ns2', 'http://authorization.wallet.de');
$elements = $xml->xpath('//S:Envelope/S:Body/ns2:authorizePlayer/authorizationRequest/callerId');
$element = $elements[0];
echo $element;

Will result in:

mycallerid

Upvotes: 0

Related Questions