how you can send a XML Soap request and read the XML response with in a PHP page.
For testing this, I have found the website holidaywebservice.com which will give response for XML request we send.
1. The below image shows the request and response for a service from the website. We send an XML request and the XML response is shown. This test is done through Postman web service tester app.
The XML request we send is shown below. First we are sending the XML request without any parameters.
<?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>
<GetCountriesAvailable xmlns="http://www.holidaywebservice.com/HolidayService_v2/" />
</soap:Body>
</soap:Envelope>
The XML Response we got 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>
<GetCountriesAvailableResponse xmlns="http://www.holidaywebservice.com/HolidayService_v2/">
<GetCountriesAvailableResult>
<CountryCode>
<Code>Canada</Code>
<Description>Canada</Description>
</CountryCode>
<CountryCode>
<Code>GreatBritain</Code>
<Description>Great Britain and Wales</Description>
</CountryCode>
<CountryCode>
<Code>UnitedStates</Code>
<Description>United States</Description>
</CountryCode>
</GetCountriesAvailableResult>
</GetCountriesAvailableResponse>
</soap:Body>
</soap:Envelope>
The PHP code to send the above XML request and read the XML response is shown below.
index.php
<?php
try{
$soapclient = new SoapClient('http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx?wsdl');
$response =$soapclient->GetCountriesAvailable();
var_dump($response);
echo '<br><br><br>';
$array = json_decode(json_encode($response), true);
print_r($array);
echo '<br><br><br>';
echo $array['GetCountriesAvailableResult']['CountryCode']['5']['Description'];
echo '<br><br><br>';
foreach($array as $item) {
echo '<pre>'; var_dump($item);
}
}catch(Exception $e){
echo $e->getMessage();
}
?>
The index.php on running on server will give you the below result.
2.Now we will try sending a request with setting 2 XML variables
The XML request we send is,
<?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>Scotland</countryCode>
<year>2018</year>
</GetHolidaysForYear>
</soap:Body>
</soap:Envelope>
The XML response for the above request is,
<?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>Scotland</Country>
<HolidayCode>NEW-YEARS-DAY-ACTUAL</HolidayCode>
<Descriptor>New Year's Day</Descriptor>
<HolidayType>Other</HolidayType>
<DateType>Actual</DateType>
<BankHoliday>NotRecognized</BankHoliday>
<Date>2018-01-01T00:00:00</Date>
<RelatedHolidayCode>NEW-YEARS-DAY-OBSERVED</RelatedHolidayCode>
</Holiday>
<Holiday>
<Country>Scotland</Country>
<HolidayCode>NEW-YEARS-EVE</HolidayCode>
<Descriptor>New Year's Eve</Descriptor>
<HolidayType>Other</HolidayType>
<DateType>ObservedActual</DateType>
<BankHoliday>NotRecognized</BankHoliday>
<Date>2018-12-31T00:00:00</Date>
</Holiday>
</GetHolidaysForYearResult>
</GetHolidaysForYearResponse>
</soap:Body>
</soap:Envelope>
The php page for sending the above request and read the response is,
index1.php
<?php
try{
$soapclient = new SoapClient('http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx?wsdl');
$param=array('countryCode'=>'Scotland','year'=>'2018');
$response =$soapclient->GetHolidaysForYear($param);
var_dump($response);
echo '<br><br><br>';
$array = json_decode(json_encode($response), true);
print_r($array);
echo '<br><br><br>';
echo '<br><br><br>';
foreach($array as $item) {
echo '<pre>'; var_dump($item);
}
}catch(Exception $e){
echo $e->getMessage();
}
?>
On running the above code, It will show the output
No comments:
Post a Comment