Php soap xml响应-如何将顽固的数据取出
我正在尝试获取Php soap xml响应-如何将顽固的数据取出,php,xml,soap,simplexml,Php,Xml,Soap,Simplexml,我正在尝试获取faultstring元素的内容,并且我对SimpleXML的对象语法($xml->…->faultstring)更为熟悉。下面的DomDocument方法可以工作,但我更喜欢使用SimpleXML PHP版本5.6.40,Libxml 2.9.1 $xmlResponse = '<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.
faultstring
元素的内容,并且我对SimpleXML的对象语法($xml->…->faultstring
)更为熟悉。下面的DomDocument方法可以工作,但我更喜欢使用SimpleXML
PHP版本5.6.40,Libxml 2.9.1
$xmlResponse = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Server did not recognize the value of HTTP Header SOAPAction: https://gw1.domain.com:4430/.</faultstring>
<detail />
</soap:Fault>
</soap:Body>
</soap:Envelope>';
// DOES NOT WORK
$xml = simplexml_load_string($xmlResponse,NULL,NULL,"http://schemas.xmlsoap.org/soap/envelope/");
$faultstring = (string) ($xml->Body->Fault->faultstring);
var_dump($faultstring);
// WORKS
$_DomObject = new DOMDocument;
$_DomObject->loadXML($xmlResponse);
if (!$_DomObject) die("Error while parsing the document");
$s = simplexml_import_dom($_DomObject);
foreach(['faultcode','faultstring','detail'] as $tag) {
echo $tag.' => '.$_DomObject->getElementsByTagName($tag)[0]->textContent.'<br/>';
}
$xmlResponse='
soap:客户端
服务器未识别HTTP标头SOAPAction的值:https://gw1.domain.com:4430/.
';
//不起作用
$xml=simplexml\u load\u字符串($xmlResponse,NULL,NULL,“http://schemas.xmlsoap.org/soap/envelope/");
$faultstring=(string)($xml->Body->Fault->faultstring);
变量转储($faultstring);
//工作
$\u DomObject=新的DOMDocument;
$\u DomObject->loadXML($xmlResponse);
如果(!$\u DomObject)死亡(“解析文档时出错”);
$s=simplexml\u import\u dom($\u DomObject);
foreach(['faultcode','faultstring','detail']作为$tag){
echo$tag.'=>。$\u DomObject->getElementsByTagName($tag)[0]->textContent.
;
}
下面是使用名称空间时的外观。您仍然可以获得对象语法,但它变得更加复杂
$xml = new SimpleXMLElement($xmlResponse);
$fault = $xml->children("soap", true)->Body->children("soap", true)->Fault->children()->faultstring;
echo (string)$fault . "\n";
两者的输出:
服务器未识别HTTP标头SOAPAction的值:https://gw1.domain.com:4430/.
我真傻,我以为“孩子”部分是含蓄的!我确实试过了,但还不够。很好的解决方案。在这里调用$xml->getnamespace(true)
是不必要的(在我看来,强制转换到对象是很奇怪的)。如果您希望依赖于文档特定的前缀不改变,只需使用->children('soap',true)
,但更好的方法是依赖名称空间URI,例如定义('XMLNS_soap','http://schemas.xmlsoap.org/soap/envelope/)
然后是->子对象(XMLNS\u SOAP)
@IMSoP强制转换为对象只是为了避免使用数组符号,但是字符串会更好。很长时间没有使用SimpleXML而不是DomDocument,因此我对它不是非常熟悉。@Tomgreat仅由于名称空间的原因才需要显式调用children()
。否则是的,这是隐含的。@miken32我一直不明白为什么人们想要“避免数组符号”-我很高兴看到stdClass
从语言中删除-但你并不孤单。更重要的一点是a)通过别名引用名称空间有一个内置的快捷方式,b)通过别名引用可以在源代码稍微更改其编码时中断,而通过URI引用只有在基础数据更改时才会中断。
$xml = new SimpleXMLElement($xmlResponse);
$fault = $xml->xpath("//faultstring");
echo (string)$fault[0] . "\n";
$xml = new SimpleXMLElement($xmlResponse);
$fault = $xml->children("soap", true)->Body->children("soap", true)->Fault->children()->faultstring;
echo (string)$fault . "\n";