Using SOAP with PHP - Creating a SOAP Client
(Page 6 of 7 )
Creating a SOAP Client to access our Server with is just as simple as creating the Server was. Understand though that the Client does not necessarily need to be a PHP Client. The SOAP Server we just created can be connected to by any type of Client, whether that be Java, C#, C++, etc.
To create the SOAP Client, all we need to do are three things.
First, include the NuSOAP library. This is done just as it was for the Server.
require_once('nusoap.php'); |
Secondly, we need to instantiate the soapclient class. We pass in the URL of the SOAP Server we are dealing with.
$c = new soapclient('http://localhost/stockserver.php'); |
Last make a call to the Web Service. The one caveat is that the parameters to the Web Service must be encapsulated in an array in which the keys are the names defined for the service. You will see that I have an array key named 'symbol' because that is the name of the input parameter of my function. If you remember how we specified the input parameters when we registered the function with the server, you will see that this is very similar.
$stockprice = $c->call('getStockQuote', array('symbol' => 'ABC')); |
Now, here is the completed Client script, which I have saved in a file named stockclient.php.
<?php require_once('nusoap.php');
$c = new soapclient('http://localhost/stockserver.php');
$stockprice = $c->call('getStockQuote', array('symbol' => 'ABC'));
echo "The stock price for 'ABC' is $stockprice.";
?> ?> |
There it is. It really is that simple.
Next: Conclusion >>
More Miscellaneous Articles
More By Matt Wade
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|