This post will show a simple example of how to interpretate a WSDL file and a very simple, yet quick example of how to extract information from this file through PHP.
In this example I assume that you’ve already have SOAP enabled in your PHP configuration because this is beyond the scope of this example. If you’re not sure, you can check your phpinfo file. There should be something like this:
Example WSDL file
For the example WSDL file we’ll take this WSDL file. It’s about the World Championship football 2010 held in South Africa.
Analyzing the WSDL file
Let’s try and print the top goal scorers of the tournament. For doing this, we can see the following on line 1295 – 1300:
We now see that the operation we should call is called TopGoalScorers. This operation expects as input a TopGoalScorersSoapRequest. We don’t know what it is yet, so let’s find out. If we search the document for this message, we get to line 999 – 1002 which says:
Right. So now we know that the TopGoalScorersSoapRequest consists of just one part (the parameters). We know that the element is called TopGoalScorers, but we do not know anything about this parameter yet. So we search the document for the element TopGoalScorers. We can find this element at line 384 – 390. These lines say
Now there we have it: we now finally know that the method TopGoalScorers which we saw in the first WSDL fragment expects one parameter as input. This parameter is called iTopN and is of the type int.
Getting to the code
Finally we can do something with the WSDL. Actually we can do this in a very short way thanks to the PHP SoapClient!
Calling the service with the parameter and obtaining the results can be done in just 2 lines of code. Let’s give it a try and obtain the top 5 goal scorers. We can do this by the following two lines:
Note that we use the information we obtained from the WSDL file here: on the $client object, we call the method TopGoalScorers and provide an array of parameters. In this case the array contains only one parameter: the iTopN parameter with an int value of 5.
The result will contain an object, so you will need to traverse the object structure.
Choose how much results
There are a lot of possibilities for abstracting such a Web Service call further. I will just give a very simple example file where you can choose how many results you want to see and visualize this in a table.
Note that the form calls index.php. This is because I called this script index.php. If you call your script differently, the action should be your script file name. (The reason why I didn’t use $_SERVER[‘PHP_SELF’] is because of this Dutch source about how this element can cause an XSS problem).
I hope you found the example useful. If not, let me know! Now go and play with PHP, SOAP and WSDL some more!
Edit 11/08/2010: Noticed that the WSDL was removed. Changed to WC 2010 WSDL file which seems to have the same format as the original WSDL.