Skip to content
Advertisement

Parse through SOAP response

I am a beginner at SOAP and have been trying to figure how to parse through XML response when I call the SOAP API.I have already tried similar questions like this on stackoverflow but somehow they are not working for me. I want to access Village and VillageName tag in a loop .

    <dsVillages>
<xs:schema id="dsVillages" targetNamespace="http://app.in/wsror/dsVillages.xsd" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="dsVillages" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="VILLAGES">
<xs:complexType>
<xs:sequence>
<xs:element name="taluka" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Village" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="VillageName" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="30"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram>
<dsVillages>
<VILLAGES diffgr:id="VILLAGES1" msdata:rowOrder="0">
<taluka>0100</taluka>
<Village>0192</Village>
<VillageName>Adcol </VillageName>
</VILLAGES>
<VILLAGES diffgr:id="VILLAGES2" msdata:rowOrder="1">
<taluka>0100</taluka>
<Village>1240</Village>
<VillageName>Bando </VillageName>
</VILLAGES>
<VILLAGES diffgr:id="VILLAGES3" msdata:rowOrder="2">
<taluka>0100</taluka>
<Village>0207</Village>
<VillageName>Beto </VillageName>
</VILLAGES>
<VILLAGES diffgr:id="VILLAGES4" msdata:rowOrder="3">
<taluka>01006</taluka>
<Village>0189</Village>
<VillageName>Betqi </VillageName>
</VILLAGES>

</dsVillages>
</diffgr:diffgram>
</dsVillages>

I am using below code to get response from API’

$url = "http://test.asmx?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); 

I am able to access 1 value at a time if I use below method

 $doc = new DOMDocument();
    $doc->loadXML($data);    
echo $doc->getElementsByTagName('VillageName')->item(1)->nodeValue;

output for above method is :

Bando

I need to access Village and VillageName tag in a loop.

Advertisement

Answer

I have accessed the required tags using below method. Plz note i’m using option tag as these will be used as dropdown options

$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> ';
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement