PHP-SOAP web service with out a WSDL
This article is going to describe how to create a simple web service with php-soap step by step.
Requirements
Server to run php (apache, xampp , wamp)
PHP-SOAP extension
Steps
Let's write a simple web service to find the length of a given string.
So here is the code for StringLengthServer.php
<?php
Run this StringLengthClient.php to receive the service.
So the output will be:
Requirements
Server to run php (apache, xampp , wamp)
PHP-SOAP extension
Steps
Let's write a simple web service to find the length of a given string.
So here is the code for StringLengthServer.php
<?php
# StringLengthServer.php
# Copyright (c) 2011 by Lakmali Baminiwatta
#
function findLength($string) {
return "Length of the string " . $string . "is : ".strlen($string);
}
$server = new SoapServer(null,
array('uri' => "urn://www.lakmali.lk"));
$server->addFunction("findLength");
$server->handle();
# Copyright (c) 2011 by Lakmali Baminiwatta
#
function findLength($string) {
return "Length of the string " . $string . "is : ".strlen($string);
}
$server = new SoapServer(null,
array('uri' => "urn://www.lakmali.lk"));
$server->addFunction("findLength");
$server->handle();
?>
- First thing done here is to write a function to perform the action done by the service. So here function findLength($string) takes a string as the argument and prints back the length of it.
- Then SoapServer is created. First argument null which means no WSDL used. Second argument is uri value is just a unique string as the namespace.
- Then we have to add the function findLength to the server.
- Now copy the file StringLengthServer.php to the server to run. Here it is assumed the url which service run is http://localhost/simple_server/StringLengthServer.php.
- Now let's write a simple client program to get the service from the webservice.
So here is the code for StringLengthClient.php
<?php
StringLengthClient.php
# Copyright (c) 2011 by Lakmali Baminiwatta
#
$client = new SoapClient(null, array(
'location' => "http://localhost/simple_server/StringLengthServer.php",
'uri' => "urn:urn://www.lakmali.lk",
'trace' => 1 ));
$return = $client->__soapCall("findLength",array("lakmali"));
echo("\nReturning value of __soapCall() call: ".$return);
echo("\nDumping request headers:\n"
.$client->__getLastRequestHeaders());
echo("\nDumping request:\n".$client->__getLastRequest());
echo("\nDumping response headers:\n"
.$client->__getLastResponseHeaders());
echo("\nDumping response:\n".$client->__getLastResponse());
?># Copyright (c) 2011 by Lakmali Baminiwatta
#
$client = new SoapClient(null, array(
'location' => "http://localhost/simple_server/StringLengthServer.php",
'uri' => "urn:urn://www.lakmali.lk",
'trace' => 1 ));
$return = $client->__soapCall("findLength",array("lakmali"));
echo("\nReturning value of __soapCall() call: ".$return);
echo("\nDumping request headers:\n"
.$client->__getLastRequestHeaders());
echo("\nDumping request:\n".$client->__getLastRequest());
echo("\nDumping response headers:\n"
.$client->__getLastResponseHeaders());
echo("\nDumping response:\n".$client->__getLastResponse());
In here the location argument of the SoapClient must be the URL to server program which we wrote above and added to run. Make sure it is correct.
Run this StringLengthClient.php to receive the service.
So the output will be:
Returning value of __soapCall() call: Length of the string lakmali is : 7 Dumping request headers: POST /simple_server/StringLengthServer.php HTTP/1.1 Host: localhost Connection: Keep-Alive User-Agent: PHP-SOAP/5.2.9 Content-Type: text/xml; charset=utf-8 SOAPAction: "urn:urn://www.lakmali.lk#findLength" Content-Length: 510 Dumping request: <soap-env:envelope soap-env:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:urn://www.lakmali.lk" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soap-env:body><ns1:findLength> <param0 xsi:type="xsd:string">lakmali</param0> </ns1:findLength></soap-env:body></soap-env:envelope> Dumping response headers: HTTP/1.1 200 OK Date: Tue, 07 Jun 2011 13:24:06 GMT Server: Apache/2.2.10 (Fedora) X-Powered-By: PHP/5.2.9 Content-Length: 550 Connection: close Content-Type: text/xml; charset=utf-8 Dumping response: <soap-env:envelope soap-env:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn://www.lakmali.lk" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soap-env:body><ns1:findLengthResponse> <return xsi:type="xsd:string">Length of the string lakmali is : 7</return> </ns1:findLengthResponse></soap-env:body></soap-env:envelope>Here the client program is written to display SOAP messages exchanged. But for a real client ony you need is return value from soapCall.
<?php
# StringLengthClient.php
# Copyright (c) 2011 by Lakmali Baminiwatta
#
$client = new SoapClient(null, array(
'location' => "http://localhost/simple_server/StringLengthServer.php",
'uri' => "urn:urn://www.lakmali.lk",
'trace' => 1 ));
$return = $client->__soapCall("findLength",array("lakmali"));
echo("\nResult: ".$return);
# Copyright (c) 2011 by Lakmali Baminiwatta
#
$client = new SoapClient(null, array(
'location' => "http://localhost/simple_server/StringLengthServer.php",
'uri' => "urn:urn://www.lakmali.lk",
'trace' => 1 ));
$return = $client->__soapCall("findLength",array("lakmali"));
echo("\nResult: ".$return);
?>
Awesome post Lakmali! This really helped me in debug mode. Also rare to find any documentation with no WSDL
ReplyDeleteThanks Matt!
ReplyDeleteHi lakmali
ReplyDeleteDont u hv PHP-SOAP web service with a WSDL example because we try to access wsdl web service in DSS using PHP.
Check this link, you'll find what you need.
Deletehttp://wso2.com/library/1060
Thank you sharing this kind of noteworthy information. Nice Post.
ReplyDeleteopencu
Education
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete
ReplyDeleteThis content of information has
helped me a lot. It is very well explained and easy to understand.
seo training classes
seo training course
seo training institute in chennai
seo training institutes
seo courses in chennai
seo institutes in chennai
seo classes in chennai
seo training center in chennai
ReplyDeleteYou write this post very carefully I think, which is easily understandable to me. Not only this, but another post is also good. As a newbie, this info is really helpful for me. Thanks to you.
Tally ERP 9 Training
tally classes
Tally Training institute in Chennai
Tally course in Chennai
Thanks for sharing this valuable information to our vision. You have posted a worthy blog keep sharing.
ReplyDeleteDigital Marketing Course In Kolkata
Web Design Course In Kolkata
SEO Course In Kolkata
Amazing Post. keep update more information.
ReplyDeleteAviation Academy in Chennai
Air Hostess Training in Chennai
Airport Management Courses in Chennai
Ground Staff Training in Chennai
Aviation Courses in Chennai
Air Hostess Training Institute in Chennai
Airline Courses in Chennai
Airport Ground Staff Training in Chennai
Nice post, I like to read this blog. It is very interesting to read.
ReplyDeletefibonacci in python
python class inheritance