|
Web Services Implementation using PHP and SOAP
Simple PHP SOAP Client Example
Below is the sample code for the client which sends the request to the remote server for
validating the string, which is passed as a parameter to the server.
Code walk through
Now we will see how the above code is processed. The above code shows how the client sends
data to the server to validate it if it's a valid string.
$string =’Hello World’;
This includes the NuSoap classes to the code:
$stringArray = array($string);
Create a variable for the string which you want to send to server. The parameter
must be passed as an array to the SOAP client.
$s= new
soapclient(‘http://localhost/working_directory/validateString.php’);
The above code instantiate the SOAP client. Local host is used since the client and server
code both are present on the same machine. If the server code is on a remote machine then the correct path of the validateString.php needs to be provided in the soap client.
$result =$s àcall (‘validateString’,$stringArray);
|
This call method tells the soap client which service we want to access and passes the
string array to the server and the method. The resulting variable stores the response from the server.
if(!$error = $sàgetError()) {echo ‘String : ‘ .$result;}else {echo ‘Error:’ .$error;} |
We will be checking the error using getErrror() method. If an error occurs this method returns the string description of the error, otherwise it returns false. In the above code we are printing our result in case of success, otherwise we print the error detail.
<? phprequire(‘nusoap.php);$s= new soap_server;$sàregister(‘validateString’);function validateString($stringValue) {if( is_string($stringValue) {return $stringValue;else {return new soap_fault(‘Client’,’’.’This is not a valid string.’);}}$sàservice($HTTP_RAW_POST_DATA);?> |
Simple PHP SOAP Server Example
$s= new soap_server;
|
Code walk through
First we instantiate the soap server using the soap_server class.
$sàregister(‘validateString’);
|
To allow our function to be called from remote location, we need to register it
with server object.
function validateString($stringValue) {if( is_string($stringValue) {return $stringValue;else {return new soap_fault(‘Client’,’’.’This is not a valid string.’);}}
|
Next we have a function definition which checks if the parameter passed to the function is a validate string or not.
$sàservice($HTTP_RAW_POST_DATA);
|
If the input string is a valid string then it will return the string, otherwise it
will return the error using soap_fault class. Finally the above method passes the incoming data from the remote client to the SOAP server’s service method. This service method processes the incoming request and calls the appropriate method.
Summary
Using PHP and NuSoap, we can develop robust and interoperable Web Services. In the above
example we have seen how PHP consumes and deploys web services with SOAP. In the
current scenario we have used local host but actually we can place our client
and server code on different machines in the network and check the code in a
distributed system.
| Comments: | ||
No Messages FoundYou can post questions/corrections/feedback here
| ||
|
If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly. | ||


