php - 从 php CURL 读取 SOAP XML
问题描述
嗨,我有这个来自肥皂服务的 xml。我用 curl 获取 xml。php中如何访问节点?结果可以有更多的resulSets
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns3:ExampleResponse xmlns:ns3="http://example.com/ptfall" xmlns:ns2="http://example.com/soa">
<return>
<ns3:resultSet>
<ns3:categoria>
<ns2:codice>1</ns2:codice>
<ns2:descrizione>Esempio xxx</ns2:descrizione>
</ns3:categoria>
<ns3:causale>
<ns3:codice>_XXXXX</ns3:codice>
<ns3:descrizione>Annullo Mancato</ns3:descrizione>
<ns3:identificativo>
<ns2:long>74</ns2:long>
</ns3:identificativo>
</ns3:causale>
</ns3:resultSet>
<ns3:serviceInfo>
<ns2:codiceErroreOccorso>0</ns2:codiceErroreOccorso>
<ns2:erroreOccorso>false</ns2:erroreOccorso>
<ns2:executionId>xxxxxxxxxxx</ns2:executionId>
<ns2:tipoErroreOccorso>0</ns2:tipoErroreOccorso>
</ns3:serviceInfo>
</return>
</ns3:ExampleResponse>
</soap:Body>
</soap:Envelope>
例如,我想为每个我尝试使用的结果集只返回 ns3:descrizione 节点,但没有用
$soap = simplexml_load_string($data);
$response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children()->ExampleResponse->........DUNNO HOW TO DO IT;
echo $response;
解决方案
它比这更复杂一些,但是 - 对于你的例子ns3:descrizione
- 它可以通过这种方式完成:
$soap = simplexml_load_string($data);
$soap->registerXPathNamespace("ns3", "http://example.com/ptfall");
$resultSet = $soap->xpath('//ns3:resultSet');
foreach ($resultSet as $rs)
{
$infos = $rs->xpath('.//*[local-name()="descrizione"]/text()');
foreach ($infos as $target) {
echo $target ."\r\n";
}
}
输出:
Esempio xxx
Annullo Mancato
推荐阅读
- java - Javafx:使用 FlowPane 的响应式设计
- javascript - 无效的网络服务调用
- javascript - 如何获取 unicode 中的 HTML 文本?
- spring-boot - Spring Boot 2.1 复制 reactiveMongoTemplate bean
- perl - 使用 =~ 运算符进行字符串比较
- c# - 使用 linq2db 联合不同类型
- javascript - 如果@ char 在使用javascript的字符串中的大括号内,如何不显示下拉菜单或做出反应?
- python - 从 Pandas 中删除依赖于其他行中的多个值的行
- javascript - 运行 Firebase Firestore 事务,包括异步 React Native 函数中的循环
- python - 在python中使用openCV调整图像大小