使用 php 解析 SOAP API

Parse SOAP API using php

我是 SOAP 的初学者,一直在想办法在我调用 SOAP 时如何获得正确的响应 API。自从过去 3 天以来,我一直在尝试 diff 方法,但仍然不知道。我已经在 Whosebug 上尝试过类似的问题,但不知何故它们对我不起作用。

非常感谢您的帮助或回复我如何能够完成这项工作。

这是我应该发送的示例请求:

POST /wsror/Form.asmx HTTP/1.1
Host: 10.159.159.1
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://apps.in/wsror/GetAllVill"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetAllVill xmlns="http://apps.in/wsror">
      <strTaluka>string</strTaluka>
      <Username>string</Username>
      <Password>string</Password>
    </GetAllVill>
  </soap:Body>
</soap:Envelope>

这是回复:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetAllVillResponse xmlns="http://apps.in/wsror">
      <GetAllVillResult>dataset</GetAllVillResult>
    </GetAllVillResponse>
  </soap:Body>
</soap:Envelope>

我尝试使用以下方法获取响应:

$soapclient = new SoapClient('sensitive info');

$param=array('sensitive info');
$response =$soapclient->GetAllVill($param);
var_dump($response);

我得到以下回应:

object(stdClass)#2 (1) { ["GetAllVillResult"]=> object(stdClass)#3 (1) { ["any"]=> string(6596) "19200Adco 24000Bandora 3001000600020700Beto 30010tqui 001900Bma 300100060Borim 600019Capar  " } } 

我用curls终于解决了这个问题。

$url = "http://apps.in/wsror?wsdl";

$xml = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetAllVill xmlns="http://apps.in/wsror">
 <strTaluka>code</strTaluka>
      <Username>Username</Username>
      <Password>Password</Password>
    </GetAllVill>
  </soap:Body>
</soap:Envelope>';

$headers = array(
    "Content-type: text/xml",
    "Content-length: " . strlen($xml),
    "Connection: close",
);

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$data = curl_exec($ch); 
//echo $data;

  $doc = new DOMDocument();
  $doc->loadXML($data);

然后遍历所需的标签。

$VILLAGES = $doc->getElementsByTagName('VILLAGES');
foreach ($VILLAGES as $VILLAGE) {
    $VillageName = $VILLAGE->getElementsByTagName('VillageName')->item(0)->nodeValue;
    $Village = $VILLAGE->getElementsByTagName('Village')->item(0)->nodeValue;
   // $id = $VILLAGE->getElementsByTagName('taluka')->item(0)->nodeValue;
 echo '<option  value="'.htmlentities($Village, ENT_QUOTES, "UTF-8").'" >'.htmlentities($VillageName, ENT_QUOTES, "UTF-8").'</option> ';
}