Press enter to see results or esc to cancel.


Send SOAP Request with custom variables and read XML response from PHP page


In this video I have shown how we can send XML request with custom variables set from a HTML form.
The XML Response received is parsed and shown in same page.

For testing this, I have found the website holidaywebservice.com which will give response for XML request we send.

1. The Sample XML Request we are going to send is shown below


<?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>
  <GetHolidaysForYear xmlns="http://www.holidaywebservice.com/HolidayService_v2/">
    <countryCode>GreatBritain</countryCode>
    <year>2019</year>
  </GetHolidaysForYear>
 </soap:Body>
</soap:Envelope>

2. The response for the above request is shown below,


<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
 <GetHolidaysForYearResponse xmlns="http://www.holidaywebservice.com/HolidayService_v2/">
  <GetHolidaysForYearResult>
   <Holiday>
    <Country>GreatBritain</Country>
    <HolidayCode>NEW-YEARS-DAY-ACTUAL</HolidayCode>
    <Descriptor>New Year's Day</Descriptor>
    <HolidayType>Other</HolidayType>
    <DateType>Actual</DateType>
    <BankHoliday>NotRecognized</BankHoliday>
    <Date>2019-01-01T00:00:00</Date>
    <RelatedHolidayCode>NEW-YEARS-DAY-OBSERVED</RelatedHolidayCode>
   </Holiday>
   <Holiday>
    <Country>GreatBritain</Country>
    <HolidayCode>NEW-YEARS-DAY-OBSERVED</HolidayCode>
    <Descriptor>New Year's Day</Descriptor>
    <HolidayType>Other</HolidayType>
    <DateType>Observed</DateType>
    <BankHoliday>Recognized</BankHoliday>
    <Date>2019-01-01T00:00:00</Date>
    <RelatedHolidayCode>NEW-YEARS-DAY-ACTUAL</RelatedHolidayCode>
   </Holiday>
  </GetHolidaysForYearResult>
</GetHolidaysForYearResponse>
</soap:Body>
</soap:Envelope>

3.The form.php page with simple HTML form to create the XML to submit data.


<html>
<title>Send SOAP XML Reqeuest using Custom varaibles</title>
<head>
<script src="https://code.jquery.com/jquery-latest.js"></script>
<script>
  function submit_soap(){
    var country=$("#country_name").val();
    var year=$("#year").val();
    $.post("callme.php",{country:country,year:year},
    function(data){
      $("#xml_response").html(data);
    });
}
</script>
</head>
<body>
  <center>
    <h3>Send SOAP XML Reqeuest using Custom varaibles</h3>
     <form id="myForm" method="post">
      Country Name : <input name="country_name" id="country_name" type="text" /><br />
      Year: <input name="year" id="year" type="text" /><br />
      <input type="button" value="Submit" onclick="submit_soap()" />
    </form>
    <br>-----------
    <div id="xml_response"></div>
   </center>
</body>
</html>

4. The XML request is send from callme.php. We take the value from the form and submit the value to the callme.php page using jquery.


<?php
try{
 $countrycode=$_POST['country'];
 $year=$_POST['year'];
 $soapclient = new SoapClient('http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx?wsdl');
 $param=array('countryCode'=>$countrycode,'year'=>$year);
 $response =$soapclient->GetHolidaysForYear($param);
 echo '<br><br><br>';
 $array = json_decode(json_encode($response), true);

  function dispaly_array_recursive($array_rec){
     if($array_rec){
       foreach($array_rec as $value){
       if(is_array($value)){
        dispaly_array_recursive($value);
       }else{
         echo $value.'<br>';
       }
   }
  }
}
  dispaly_array_recursive($array);
}catch(Exception $e){
    echo $e->getMessage();
}
?>

4. The form and result is shown as below.


Tags

Comments

Comments are disabled for this post