2012-09-03 97 views
0

这里是一个API我的XML响应节点:挣扎读取用SimpleXML

<?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> 
    <GetCertificateResponse xmlns="http://url.com"> 
     <GetCertificateResult> 
     <ReturnValue xmlns=""> 
      <Status>Success</Status> 
      <Message/> 
      <CertificateNumber/> 
      <URL/> 
     </ReturnValue> 
     </GetCertificateResult> 
    </GetCertificateResponse> 
    </soap:Body> 
</soap:Envelope> 

我怎样才能reaq状态节点?我试过这么多的连击:

  $getCertificateXMLResponse = simplexml_load_string($getCertificateXMLResponse); 

     echo $getCertificateXMLResponse->GetCertificateResponse->GetCertificateResult->ReturnValue->Status; 
+0

使用SOAP PHP extention处理SOAP请求。 –

回答

1

您还可以使用XPath做

$xml = simplexml_load_string($soap, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/"); 
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/'); 

$result = $xml->xpath('//Status'); 
echo $result[0]; 
+0

你能告诉我为什么我的方法不工作吗?是因为它是SOAP,所以它比阅读正常的XML响应更重要吗?谢谢! –

+0

这是因为你没有在树的头部开始你的方法。 在你的例子中,你应该使用类似于: $ getCertificateXMLResponse-> children(“soap”,true) - > Body-> children() - > GetCertificateResponse-> GetCertificateResult-> ReturnValue->状态 –

+0

原始解决方案仅适用于这对我来说足够了!欢呼输入 –

0

是......丑..但工程

$xml = new SimpleXMLElement(file_get_contents('a.xml'),0,false,''); 
$namespaces = $xml->getDocNamespaces(true); 
$xml->registerXPathNamespace('empty', $namespaces['']); 
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/'); 
$status = array_shift($xml->xpath("soap:Body/empty:GetCertificateResponse/empty:GetCertificateResult/ReturnValue/Status")); 
echo $status->asXML(); 

我很好奇,看看更优雅的解决方案。

确实看看像nusoap这样的东西。