[英]Read soap xml with php
Anyone can help on this how I can get the value of callerId from this string? 任何人都可以提供帮助,如何从该字符串中获取callerId的值?
String is pulled from php://input
and i need this with just php without soap class and only this one value no loop. 字符串是从php://input
拉出的,我只需要不带soap类的php和仅此值的无循环就可以了。
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>
You can easily use DOMDocument
. 您可以轻松使用DOMDocument
。
Assuming your string is in $xml
variable, try this code: 假设您的字符串在$xml
变量中,请尝试以下代码:
$dom = new DOMDocument();
$dom->loadXML( $xml, LIBXML_NOBLANKS );
$callerId = $dom->getElementsbyTagName( 'callerId' )->item(0)->nodeValue;
Another option is to use the SimpleXMLElement's xpath method and register the namespace. 另一个选择是使用SimpleXMLElement的xpath方法并注册名称空间。
$element
is of type SimpleXMLElement and you can call its __toString() method to return the string content: $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;
Will result in: 将导致:
mycallerid mycallerid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.