Home » Technology » PHP » A PHP Example For Querying Data From A SOAP Web Service

A PHP Example For Querying Data From A SOAP Web Service

While brushing up on Web Services recently I looked for a good PHP example that demonstrated querying for data from a SOAP web service. I found the following on Patrick’s Playground. It inspired me to write about the subject myself. Functionally with this example the two technologies that we are looking at are PHP and SOAP.  SOAP, or Simple Object Access Protocol utilizes XML as it’s format and, in our examples, the data is transmitted via HTTP. As such, I presume a basic understanding of PHP; which implies in my mind a further understanding of  HTTP, and general Markup as you might find in HTML or XML.

For starters

For this example, your PHP installation will need to have SOAP enabled. If you are unsure; use a simple phpinfo() file to check and see if it is enable (you can read here on how to create one).  You are looking for the following lines:

PHP SOAP enabled.

Enabling SOAP for your environment is beyond the scope of this article; the steps will be platform specific. You can read an article here with steps for a CENTOS deployment.

The WSDL file

Our query against the SOAP web service will be dictated by the WSDL file provided for the web service in question. The WSDL file provides a contract, or user manual, listing the available functions we can call, along with information such as input data we can provide, and data we can expect to be returned.

I will reuse for my initial example, the WSDL file here, as referenced in the article on Patrick’s Playground. The file can be viewed in your browser by following the link, or downloaded and opened for analysis using Notepad, or Notepad++ in a Microsoft Windows environment:

http://footballpool.dataaccess.eu/data/info.wso?wsdl

Reviewing the WSDL file, we can see a number of functions; here is a simple one with one input variable:

<xs:element name=”AllMidFields>
<xs:complexType>
<xs:sequence>
<xs:element name=”sCountryName type=”xs:string/>
</xs:sequence>
</xs:complexType>
</xs:element>
The parent tag lists the function name: AllMidFields; and the input is a String, sCountryName. It is that simple; there is a function AllMidFields that takes a single input, the Country Name (sCountryName). Here is what the code looks like to call this function and a simple example of displaying the results:

<?php

$countryName = “Australia”;
$client = new SoapClient(“http://footballpool.dataaccess.eu/data/info.wso?wsdl”);
$results = $client->AllMidFields(array(‘sCountryName’ => $countryName));

$resultsArray = $results->AllMidFieldsResult->string;
echo “<ul>”;
for($i = 0; $i < sizeOf($resultsArray); $i++) {
echo “<ol>$resultsArray[$i]</ol>”;
}
echo “</ul>”;
?>

 

And these are the results when run:

      Ben Halloran
      Dario Vidosic
      James Holland
      James Troisi
      Mark Bresciano
      Mark Milligan
      Massimo Luongo
      Matt McKay
      Mile Jedinak
      Oliver Bozanic
      Tommy Oar

 

As the code was put together, you will need to know what your results are before you can adequately write code to process it. Therefore var_dump was used on the $results variable to see exactly what was being returned. Use whatever methodology you are used to for analyzing the returned values.

This example is very simple; much more complex examples exist. It is hoped that this provides at least the foundation for future exploration.

Works Cited

  1. van Kouteren, Patrick . “Simple PHP SOAP example.” Patrick’s playground. 025 Mar. 2009. Mon. 19 Oct. 2015. <http://www.vankouteren.eu/blog/2009/03/simple-php-soap-example/>
All content is © copyrighted by the owner of this website, 2014 and beyond | Check us out on Twitter