Wednesday, July 18, 2012

Hello World With PEAR::SOAP

After successfully installing PEAR::SOAP, the next step is to write a 'Hello World' using the implementation. To implement a SOAP server using PEAR::SOAP, you need to define a class that describes the server, and then pass an instance of this class over to PEAR::SOAP. PEAR::SOAPs SOAP_Server class will then use the characteristics of this class to set up a new SOAP service.

pearsoap-hello-client.php

pearsoap-hello-client.php is our PEAR::SOAP version of the Hello World consumer. Below youll find a listing and description of this script.
The SOAP_Client class is PEAR::SOAPs answer to NuSOAPs soapclient, and ezSOAPs eZSOAPClient classes. We need to include the definition of this class before we can proceed:
<?
    /* Include PEAR::SOAP's SOAP_Client class: */
    require_once('SOAP/Client.php');
SOAP_Clients requires one argument: the URL to the SOAP service we wish to connect to. Optionally, you can also pass an second boolean true to the script, which would indicate that the first argument is not an URL to a service, but a path to a WSDL:
    /* Create a new SOAP client using PEAR::SOAP's SOAP_Client-class: */
    $client = new SOAP_Client('http://localhost/soap/pear-soap/pearsoap-hello-server.php');

As usual, we send a single string to the server:
    /* Define the parameters we want to send to the server's helloWorld-function.
       Note that these arguments should be sent as an array: */
    $params = array('inmessage'=>'World');

Next, we make a call to the server, telling it we want to some assistance from its helloWorld method with the data in $params. When the request is carried out, the result gets stored in $response:
    /* Send a request to the server, and store its response in $response: */
$response = $client->call('helloWorld',$params,array('namespace'=> 'urn:helloworld'));

Print the result to the user:
    /* Print the server-response: */
    print $response;
?>

No comments:

Post a Comment