2011-05-04 140 views
1

最近几天我试图解析soap响应(通过curl命令行),但我无法让它工作。我只想得到ResourceIdentifier的对象值,即rs-1304500829200-200。我使用PHP 5.3.x如何在PHP中解析SOAP响应

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <ns2:deliverMDRecordsResponse xmlns:ns2="http://mdstore.data.dnetlib.eu/" xmlns:ns3="http://www.w3.org/2005/08/addressing"> 
     <return> 
     <ns3:Address>http://129.70.12.20:8282/dnet-mdstore/services/MDStoreResultSet</ns3:Address> 
     <ns3:ReferenceParameters> 
      <ResourceIdentifier:ResourceIdentifier xmlns:ResourceIdentifier="http://www.driver.org" xmlns:wsa="http://www.w3.org/2005/08/addressing">rs-1304500829200-200</ResourceIdentifier:ResourceIdentifier> 
     </ns3:ReferenceParameters> 
     </return> 
    </ns2:deliverMDRecordsResponse> 
    </soap:Body> 
</soap:Envelope> 

<? 
$response=<<<END ....soap response here... END; 

$xml = simplexml_load_string($source); 
$xml->registerXPathNamespace('t', 'http://www.driver.org'); 
foreach ($xml->xpath('//t:ResourceIdentifier') as $item)  
{ // print_r($item);  
    echo $item->asXML();  
}  
?> 
+0

请参阅:http://stackoverflow.com/editing-help – Treffynnon 2011-05-04 11:54:54

+0

“print_r”的输出是什么? – 2011-05-04 11:55:46

+0

为什么选择CURL而不是http://www.php.net/manual/en/book.soap.php? – Treffynnon 2011-05-04 11:56:40

回答

4

你真正应该使用内置的SoapClient class如果可以的话,或者如果你不能,使用PEAR SOAP libraries。在运行PHP 5.3时,SoapClient应该可用。

在您使用Xpath的问题上,您正在查询错误的名称空间和元素。你正在查询的元素的命名空间是“http://www.driver.org”,因此这应该工作,但请记住,我没有实际运行它,但它应该是正确的:

<?php 
$xml = simplexml_load_string($response); 
$xml->registerXPathNamespace('rid', 'http://www.driver.org'); 
foreach ($xml->xpath('//rid:ResourceIdentifier') as $item) { 
    echo (string) $item; 
} 
?> 

但请不要这么做,请使用我提到的两个SOAP客户端之一。我不知道你从哪里得到{http://apilistener.envoyservices.com}payment,因为它没有在响应中提到。

+1

这种恶意软件太苛刻了!我无法在我的答案中看到任何可以证明它的理由。 – 2011-05-04 12:09:26

+0

我在您的评论前删除了我的投票,因为我看到了您的修改。清除你的缓存:)我之前忘了留下一个评论来描述投票。你没有解决这个问题,只是评论。 – Treffynnon 2011-05-04 12:13:53

+0

很酷。我养成了一个习惯,首先对那些误入歧途的人提出警告,然后,如果他们完全按照他们的计划行事,告诉他们出了什么问题。我不认为评论是与人们讨论他们提出的问题(格式,短语,细节)而不是任何答案的地方,我会告诉他们使用经过良好测试的图书馆作为后者。 – 2011-05-04 12:27:19