Read soap xml with php

983

Anyone can help on this how I can get the value of callerId from this string?
String is pulled fromphp://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>
303

Answer

Solution:

You can easily useDOMDocument.

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;

eval.in demo


373

Answer

Solution:

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

People are also looking for solutions to the problem: php - I lose my twitter Access Token every time when I'm trying to unfollow users

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.