Using SOAP, in contrast to the process of implementing a REST client or server, is quite easy as there is a PHP SOAP extension that provides both capabilities.
A frequently asked question is "what is the difference between SOAP and REST?" SOAP uses XML internally as its data format. SOAP uses HTTP but only for transport, and otherwise has no awareness of other HTTP methods. REST directly operates HTTP, and can use anything for data formats, but JSON is preferred. Another key difference is that SOAP can operate in conjunction with a WSDL, which makes the service self-describing, thus more publicly available. Thus, SOAP services are often offered by public institutions such as national health organizations.
For this example, we will make a SOAP request for an existing SOAP service offered by the United States National Weather service:
$wsdl = 'http://graphical.weather.gov/xml/SOAP_server/' . 'ndfdXMLserver.php?wsdl';
soap client instance using the WSDL:$soap = new SoapClient($wsdl, array('trace' => TRUE));$units = 'm'; $params = ''; $numDays = 7; $weather = ''; $format = '24 hourly'; $startTime = new DateTime();
LatLonListCityNames() SOAP request, identified as an operation in the WSDL, for a list of cities supported by the service. The request is returned in XML format, which suggests creating a SimpleXLMElement instance:$xml = new SimpleXMLElement($soap->LatLonListCityNames(1));
array_combine() PHP function to create an associative array where latitude/longitude is the key, and the city name is the value. We can then later use this to present an HTML SELECT drop-down list, using asort() to alphabetize the list:$cityNames = explode('|', $xml->cityNameList);
$latLonCity = explode(' ', $xml->latLonList);
$cityLatLon = array_combine($latLonCity, $cityNames);
asort($cityLatLon);$currentLatLon = (isset($_GET['city'])) ? strip_tags(urldecode($_GET['city'])) : '';
NDFDgenByDay(). We can determine the nature of the parameters supplied to the SOAP server by examining the WSDL:<message name="NDFDgenByDayRequest"> <part name="latitude" type="xsd:decimal"/> <part name="longitude" type="xsd:decimal"/> <part name="startDate" type="xsd:date"/> <part name="numDays" type="xsd:integer"/> <part name="Unit" type="xsd:string"/> <part name="format" type="xsd:string"/> </message>
$currentLatLon is set, we can process the request. We wrap the request in a try {} catch {} block in case any exceptions are thrown:if ($currentLatLon) {
list($lat, $lon) = explode(',', $currentLatLon);
try {
$weather = $soap->NDFDgenByDay($lat,$lon,
$startTime->format('Y-m-d'),$numDays,$unit,$format);
} catch (Exception $e) {
$weather .= PHP_EOL;
$weather .= 'Latitude: ' . $lat . ' | Longitude: ' . $lon;
$weather .= 'ERROR' . PHP_EOL;
$weather .= $e->getMessage() . PHP_EOL;
$weather .= $soap->__getLastResponse() . PHP_EOL;
}
}
?>Copy all the preceding code into a chap_07_simple_soap_client_weather_service.php file. You can then add view logic that displays a form with the list of cities, as well as the results:
<form method="get" name="forecast"> <br> City List: <select name="city"> <?php foreach ($cityLatLon as $latLon => $city) : ?> <?php $select = ($currentLatLon == $latLon) ? ' selected' : ''; ?> <option value="<?= urlencode($latLon) ?>" <?= $select ?>> <?= $city ?></option> <?php endforeach; ?> </select> <br><input type="submit" value="OK"></td> </form> <pre> <?php var_dump($weather); ?> </pre>
Here is the result, in a browser, of requesting the weather forecast for Cleveland, Ohio:

For a good discussion on the difference between SOAP and REST, refer to the article present at http://stackoverflow.com/questions/209905/representational-state-transfer-rest-and-simple-object-access-protocol-soap?lq=1.