簡體   English   中英

如何遍歷肥皂與卷曲請求在PHP響應?

[英]how to traverse soap response with curl request in php?

如何在php中獲取soap響應的標簽內的值。響應是這樣的。

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)

要獲取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"
  }
...
}

PHP本身集成了一些SOAP功能 如果可以利用它,則可以完全刪除CURL + SimpleXMLElement的使用。 像這樣(未經測試):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM