Read soap xml with php


Read soap xml with php

>任何人都可以帮忙,我如何从这个字符串中获取callerId的值?
字符串是从php://input中提取的,我需要这个,只有 php 没有 soap 类,只有这个值没有循环。
知道吗?

<?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>

您可以轻松使用 DOMDocument .

假设您的字符串位于变量$xml,请尝试以下代码:

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

3v4l.org 演示

<小时 />
  • 阅读更多 关于 DOMDocument

另一种选择是使用 SimpleXMLElement 的 xpath 方法并注册命名空间。

$element的类型是 SimpleXMLElement,你可以调用它的 __toString(( 方法来返回字符串内容:

$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;

将导致:

米卡勒里德