Iterate through results using the generated WsdlClass class extra methods

  06 July 2013       This post has 1 comment       By Mikaël DELSOL

Requirements

Your server must run the latest PHP version with the native SoapClient class.

For this sample, we use the generated package associated to the Data Access Football Pool  Web service. This Web service is very easy and does not require any authentication credentials.

The aim of this sample is to show you how to easily iterate through results in order to generate an HTML view of these results. The results are acquired calling a simple operation that does not require sophisticated parameters.

The generated package includes :

  • the sample file: the file that shows you how to start using the generated package
  • the autoload file: the file which loads all the generated class with the good path
  • the main WsdlClass class: the class from which each generated class inherits in order to inherit usefull methods and generic methods to deal with the SoapClient
  • the ClassMap class: the class which defines the mapping between native Football data access Web service structs and generated classes
  • all the classes required to communicate with the Football data access SOAP API

If you have a good IDE, then it should be really easy to navigate through all these numerous classes.

Take advantage of the generated WsdlClass class extra methods

First step: load classes and set configuration

The generated package includes an autoload file which makes easy to load all the generated classes at once.

require_once __DIR__ . '/FootballPoolAutoload.php';

We suppose your file is located in the root directory of the extracted package you just downloaded.

When all classes are loaded, you can define the configuration to call the SOAP API. This configuration is required to instantiate any FootballPool Service class.

$wsdl = array(); // we use the WSDL latest version $wsdl[FootballPoolWsdlClass::WSDL_URL] = 'http://footballpool.dataaccess.eu/data/info.wso?WSDL'; $wsdl[FootballPoolWsdlClass::WSDL_CACHE_WSDL] = WSDL_CACHE_NONE; $wsdl[FootballPoolWsdlClass::WSDL_TRACE] = true;

Second step: call the operation AllPlayerNames and generate a HTML view

Calling the operation is pretty trivial and is then done by calling the FootballPoolServiceAll::AllPlayerNames() method using the configuration set previously as the $wsdl variable and by passing the correct parameter to the method.

To have an easy view of the results, we are going to generate a HTML table (not perfect as we don't generate an entire valid HTML page structure) such as:

  1. First column: the player position in the results
  2. Second column: the player id value
  3. Third column: the player name
  4. Fourth column: the player country flag image
  5. Fifth column: the player large country flag image

From the second to the fifth column, the values are retrieved from each player name returned by the call.

Now, let's look to the code below:

$footballPoolServiceAll = new FootballPoolServiceAll($wsdl); if($footballPoolServiceAll->AllPlayerNames(new FootballPoolStructAllPlayerNames(true))) { // get the results array $result = $footballPoolServiceAll->getResult()->getAllPlayerNamesResult(); // call the "magic" method FootballPoolWsdlClass::initInternArrayToIterate() // in order to initialize the array that wil be used to iterate through the results // This method only works for classes that are named with the word "array" and // have the getAttributeName() method defined returning the unique property name of the class $result->initInternArrayToIterate(); echo '<table border="1" cellpadding="0" cellspacing="0">'; // display the results count returned by the call using the count() method // the count() method comes from the inheritance from the Countable interface // Please read http://php.net/manual/en/class.countable.php echo '    <caption>' . $result->count() . ' player names have been returned</caption>'; echo '    <thead>'; echo '        <tr>'; echo '            <th><stronng>Key</stronng></th>'; echo '            <th><stronng>Id</stronng></th>'; echo '            <th><stronng>Name</stronng></th>'; echo '            <th><stronng>Country Name</stronng></th>'; echo '            <th><stronng>Country Flag</stronng></th>'; echo '            <th><stronng>Country Flag Large</stronng></th>'; echo '        </tr>'; echo '    </thead>'; echo '    <tbody>'; // iterate through the results while there is one // the valid() method comes from the inheritance from the Iterator interface // Please read http://php.net/manual/en/class.iterator.php while($result->valid()) { // get the current result using the current() method // the current() method comes from the inheritance from the Iterator interface $currentPlayerName = $result->current(); // display the current result table line echo '    <tr style="text-align:center;">'; // display the iterable current key using the key() method // the key() method comes from the inheritance from the Iterator interface echo '        <td>' . $result->key().'</td>'; // display the current result own data echo '        <td>' . $currentPlayerName->getIId().'</td>'; echo '        <td>' . $currentPlayerName->getSName() . '</td>'; echo '        <td>' . $currentPlayerName->getSCountryName() . '</td>'; echo '        <td><img src="' . $currentPlayerName->getSCountryFlag() . '" alt="" /></td>'; echo '        <td><img src="' . $currentPlayerName->getSCountryFlagLarge() . '" alt="" /></td>'; echo '    </tr>'; // don't forget to call the next() method otherwise you'll have an infinite loop // the next() method comes from the inheritance from the Iterator interface $result->next();     }     echo '    </tbody>';     echo '</table>'; } else     print_r($footballPoolServiceAll->getLastError());

When we look to the XML response, we have this:

<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <m:AllPlayerNamesResponse xmlns:m="http://footballpool.dataaccess.eu"> <m:AllPlayerNamesResult> <m:tPlayerNames> <m:iId>16</m:iId> <m:sName>Adam Matuszczyk</m:sName> <m:sCountryName>Poland</m:sCountryName> <m:sCountryFlag>http://footballpool.dataaccess.eu/images/flags/pl.gif</m:sCountryFlag> <m:sCountryFlagLarge>http://footballpool.dataaccess.eu/images/flags/pl.png</m:sCountryFlagLarge> </m:tPlayerNames> <m:tPlayerNames> <m:iId>588</m:iId> <m:sName>Adil Rami</m:sName> <m:sCountryName>France</m:sCountryName> <m:sCountryFlag>http://footballpool.dataaccess.eu/images/flags/fr.gif</m:sCountryFlag> <m:sCountryFlagLarge>http://footballpool.dataaccess.eu/images/flags/fr.png</m:sCountryFlagLarge> </m:tPlayerNames> <m:tPlayerNames> <m:iId>17</m:iId> <m:sName>Adrian Mierzejewski</m:sName> <m:sCountryName>Poland</m:sCountryName> <m:sCountryFlag>http://footballpool.dataaccess.eu/images/flags/pl.gif</m:sCountryFlag> <m:sCountryFlagLarge>http://footballpool.dataaccess.eu/images/flags/pl.png</m:sCountryFlagLarge> </m:tPlayerNames> etc. </m:AllPlayerNamesResult> </m:AllPlayerNamesResponse> </soap:Body> </soap:Envelope>

When we look to the PHP response, we have this:

FootballPoolStructAllPlayerNamesResponse Object ( [AllPlayerNamesResult] => FootballPoolStructArrayOftPlayerNames Object ( [tPlayerNames] => Array ( [0] => FootballPoolStructTPlayerNames Object ( [iId] => 16 [sName] => Adam Matuszczyk [sCountryName] => Poland [sCountryFlag] => http://footballpool.dataaccess.eu/images/flags/pl.gif [sCountryFlagLarge] => http://footballpool.dataaccess.eu/images/flags/pl.png ) [1] => FootballPoolStructTPlayerNames Object ( [iId] => 588 [sName] => Adil Rami [sCountryName] => France [sCountryFlag] => http://footballpool.dataaccess.eu/images/flags/fr.gif [sCountryFlagLarge] => http://footballpool.dataaccess.eu/images/flags/fr.png ) [2] => FootballPoolStructTPlayerNames Object ( [iId] => 17 [sName] => Adrian Mierzejewski [sCountryName] => Poland [sCountryFlag] => http://footballpool.dataaccess.eu/images/flags/pl.gif [sCountryFlagLarge] => http://footballpool.dataaccess.eu/images/flags/pl.png ) [3] => etc. ) ) )

Then the HTML table should look like this:

368 player names have been returned
KeyIdNameCountry NameCountry FlagCountry Flag Large
0 16 Adam Matuszczyk Poland
Poland Country Flag - WSDL to php
Poland Country Flag Large - WSDL to php
1 588 Adil Rami France
France Country Flag - WSDL to php
France Country Flag Large - WSDL to php
2 17 Adrian Mierzejewski Poland
Poland Country Flag - WSDL to php
Poland Country Flag Large - WSDL to php
etc. etc. etc. etc. etc. etc.

Otherwise, you have to analyze the last error catched when the FootballPoolServiceAll::AllPlayerNames() method has been called by calling the generic FootballPoolWsdlClass::getLastError() method.

Conclusion

As you can see, it's pretty easy to iterate through the results by calling only one method in order to initialize the process. After calling the initInternArrayToIterate() method, you can call any method defined by each of these interfaces:

  • Countable: count()
  • Iterator: current(), next(), key(), valid() and rewind()
  • ArrayAccess: offsetExists(), offsetGet(), offsetSet(), offsetUnset()

We hope you'll find this sample useful. Don't hesitate posting a comment for this article so we can improve even more the features provided by the generated WsdlClass. You can also ask questions if something does not work properly with your results.


This post has 1 comment:

The February 20, 2015 at 12:01:08, Tom Forsell wrote:
Newbie

What is a method to obtain the response in xml format, iterated from top to bottom already?


Total 1 comments