2015-05-27 127 views
0

如何獲取肥皂響應標籤內的值在PHP中。響應是這樣的。如何在php中通過curl請求遍歷肥皂響應?

string '<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<soap:Body> 
<GetListResponse xmlns="http://test.org/"> 
<GetListResult>[{"Id":30,"Name":"OFFICE"},{"Id":31,"Name":"KUMAR KHATRI"},{"Id":32,"Name":"ASHA MAIYA SHRESTHA"},{"Id":33,"Name":"RABINDRA GHIMIRE"},{"Id":34,"Name":"CHABBI GHIMIRE"},{"Id":35,"Name":"RAJ KUMAR SHRESTHA"},{"Id":36,"Name":"RABINDRA BDH. RO'... (length=614) 

回答

1

爲了讓你可以做這樣的GetListResult值:

$source = <<<EOS 
<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<soap:Body> 
<GetListResponse xmlns="http://test.org/"> 
<GetListResult> 
[{"Id":30,"Name":"OFFICE"},{"Id":31,"Name":"KUMAR KHATRI"},{"Id":32,"Name":"ASHA MAIYA SHRESTHA"},{"Id":33,"Name":"RABINDRA GHIMIRE"},{"Id":34,"Name":"CHABBI GHIMIRE"},{"Id":35,"Name":"RAJ KUMAR SHRESTHA"}] 
</GetListResult> 
</GetListResponse> 
</soap:Body> 
</soap:Envelope> 
EOS; 

// Create simple XML element 
$xml = new SimpleXMLElement($source); 
$xml->registerXPathNamespace('test', 'http://test.org/'); 

// Get value of first "GetListResponse" element 
$result = (string)$xml->xpath('//test:GetListResult')[0]; 

// Parse JSON 
$values = json_decode($result, true); 
var_dump($values); 

輸出:

array(6) { 
    [0]=> 
    array(2) { 
    ["Id"]=> 
    int(30) 
    ["Name"]=> 
    string(6) "OFFICE" 
    } 
    [1]=> 
    array(2) { 
    ["Id"]=> 
    int(31) 
    ["Name"]=> 
    string(12) "KUMAR KHATRI" 
    } 
... 
} 

也有一些SOAP functionality integrated in PHP itself。如果你可以使用它,你將能夠完全移除CURL + SimpleXMLElement的使用。像這樣(未經測試):

$soapClient = new SoapClient("http://test.org/wsdl?WSDL"); 
$soapResult = $soapClient->SomeFunction(array('foo'=>'bar', 'baz'=>'fez')); 
$result = $soapResult->GetListResponse->GetListResult; 
+0

非常感謝!它工作正常。 –