A small tutorial about sending PHP Objects via S.O.A.P. WebServices. The Objects are of an uknown type and must be constructed dinamycally.
In the past, i have wrote an article about using the NuSoap library to create a server and a client that accepts requests and sends responses via SOAP Web Services. There are alternatives to NuSoap when it comes to making SOAP requests but this library is the most common one. However, the documentation for it is very poor and a php beginner might have problems in understanding and going through it. Even if you are a php beginner or an advanced programmer (not necessarily php!), after you’ve read this article, you might find out some useful things that you may include in your future project. The thesis of this article is to make a NuSoap client that makes a request to a SOAP server, knowing only the parameters to send, the method to use and the WSDL url, without knowing what the method, WSDL url or parameters values are.
1. Creating a basic NuSoap client
First of all, lets create our workspace.
Create a folder named NuSoap_Sample. Inside of it put your copy of nusoap.php (download link here)
Create a file in your favorite PHP editor named myClient.php inside the “NuSoap_Sample” folder . Add the following lines inside of it:
<?php
class SampleObject {
public $name = '';
}
require_once('nusoap.php');
$wsdl = 'My Sample WSDL';
$client = new nusoap_client($wsdl , true);
$proxy = $client->getProxy();
$obj = new SampleObject;
$obj->name = 'Andrew';
$result = $proxy->whatIsMyName($obj);
echo "<pre>";
print_r($result);
echo "</pre>";
?>
Ok so lets talk about what this script does:
- We include the nusoap library
- We assign the WSDL URL to the variable $wsdl (you need to get this from your service provider. An example is here)
- We make a new NuSoap client with the $wsdl file from above.
- In order for our script to work on all environments, we force it to use a proxy via the getProxy method.
- We want to send an object via the Web Service so we create a new object of our SampleObject and set our name to the $name attribute of this object.
- Lastly, we make the request and print our the result (in a pretty string)
This is very nice but…Lets see the downside of this example: If we want to send a complex object via the Web Service, we need to provide each parent attribute with a child class and create an object of each one. This will lead to a lot of files and a lot of code to be written. Furthermore, we need these classes defined which again puts us in trouble if we don’t know these and thus we cannot code them.
Can there be no other way to resolve this?!
2. Getting our hands dirty

Let’s imagine a hypothetical case where we want to make a service that sends SOAP requests via a request from a user. Lets focus on this and explain in more detail what we want to achieve: We want ANY user to be able to make requests without us caring about what the content is or where the user wants to send his request.
This being said, the above example would obviously fail since we won’t know what classes to define or what objects to create. Also, we won’t know what attributes to assign to each class/object. We might as well ask each client to wait 24 hours and start adding their classes in a file and then when they want to modify, they need to contact us via mail or phone and we can change their class files. Or wait…
Lets assume we present the user a form where they can input the WSDL address and method to use in the Web Service, and allow them to add any number of fields for the request. So we would have something like this:
$wsdl = 'user_inputed_WSDL';
$method = 'user_inputed_METHOD';
$params = array('field1 => 'value1', 'field2' => 'value2');
We would now need to make a new object of the $method string. We would need to have a class named as $method says so, and that class to have attributes as $params says. Furthermore, if the service provider wants us to send complex data, and we need to encode the field1 and field2 into a <paramaters> tag then we are really in a bad situation.
But what happens if we do the following?
(to fulfill both of the cases explained above, we will assume we have a $encode variable with the value set by the user (example ‘<parameters>’ and we want our parameters to be wrapped in this variable) :
<?php
$wsdl = 'user_inputed_WSDL';
$method = 'user_inputed_METHOD';
$params = array('field1 => 'value1', 'field2' => 'value2');
$encode = '<parameters>';
require_once('nusoap.php');
$client = new nusoap_client($wsdl , true);
$proxy = $client->getProxy();
$params = !empty($encode) ? array( $encode => $params); : $params;
$obj = (object) $params;
$result = $proxy->$method($obj);
echo "<pre>";
print_r($result);
echo "</pre>";
?>
Wow! It sent our request exactly how we want it! Field1 and field2 were encoded in a <parameters> tag and the script did not fail since we did not create a new object.
Let me explain what we did here:
1. We setup our known parameters (the WSDL url, the method to use, the parameters to send and the encode tag to use.
2. We make a new NuSoap client based on the WSDL provided
3. We setup or parameters to become a nested array where the primary key is the encoded tag and its value are the fields we want to send (if we have an encode parameter)
4. We fake create an object based on the resulting $params array.
5. We make the request via the method provided sending our fake object to the service provider.
3. Conclusion

Although the service providers usually expect an object to be sent via their Web Services, remember that the definition of Web Service tells us that the server needs not to care about the programming language the requests are coming from and needs to respond in the same manner (thus the hard to understand XML RPC system). What does this mean? It means that, if we send a request that contains an object then the whole system will work. It DOESN’T matter how we accomplish this as long as the request is good. If we want to send arrays or pure objects, its on our side to care not on the Web Service server side.
So from writing TONS of complex code just to make an ordinary request to a web service provider, you can now just use this method in an easier way (arrays are the good thing our programming heaven gave us) and much easier to debug.
Now go and test it yourself and try to variate your tests in order to fully understand the system.
