用php

时间:2016-02-13 00:25:57

标签: php xml soap

任何人都可以帮助我如何从这个字符串中获取callerId的值?
字符串是从php://input中提取的,我需要这个只有没有肥皂类的php,只有这一个值没有循环 有什么想法吗?

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

2 个答案:

答案 0 :(得分:0)

您可以轻松使用DOMDocument

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

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

eval.in demo

答案 1 :(得分:0)

另一种选择是使用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;

将导致:

  

mycallerid