WebServices in PHP

So, you’ve to consume webservices integrating all this in a simple php app. There’re tonns of docs but you don’t know where to start. So, let’s start!

 

A WS is an application accessible on the web thru given rules (ex. a simple WS can perform the sum of two number).

 

To use a WS you need to know how to use it, which parameters take on input, and what returns. The WSDL of the application contains this information.

 

More info on Creating And Consuming Web Services In Php 5

 

Now let’s stumble upon some code. To use a webservice (aka consume it) we can use SoapClient php class, described in http://it.php.net/soap

 
// create a new soapclient  object
$wsdl = "http://localhost/myservice/wsdl";
$wsapi = new SoapClient($wsdl, array("admin", "password"));

 

SoapClient loads the WSDL (it’s an xml file) thus learning

  • the list of methods
  • the url of each method
  • the signature of each method (params in, params out)

 

It eventually authenticates agains webservices with “login” and “password”.

 

If the WSDL contains the following information:

  • method: add(a,b)
    • url: http://localhost/myservice/add
    • params: a: integer, b: integer
    • return: x: integer

then we can invoke directly from php (withouth further definitions):

 
// now it's all defined! the wsdl did all the job
$sum = $wsapi->add("1","3");
echo "$sum";

 

Enjoy!