A “Web Service” is a bunch of code that resides on a server in an intranet or the internet. It performs some function and returns some data required by the client application that calls it. e.g. A foreign exchange web service might return the rates at which euros are converted to US dollars. A weather web service will return local temperature during any day. These web services can then be used in client applications like an e-commerce site that shows product prices in euros or dollars depending on the country the buyer is in, etc.
One common use of web services is in smartphone apps as a backend. A smartphone does not typically query a site’s database directly since it is not good to store database credentials locally on it. So the app calls a web service residing on a server. This web service fetches data from a web application and returns the data found to the smartphone app. e.g. An mobile e-commerce store allows users to search products by keywords. This app will call a web service which acts like a function that will accept the search keyword as a parameter. The web service returns a list of products to the calling app either as JSON or XML. The calling app will parse the results and display a list of products with names and images to the buyer.
But how is this achieved from the coding standpoint ? How does one create a PHP web service? There are many ways but I shall discuss about SOAP-based web services and illustrate with an example containing both the web service and the calling code in PHP. I will be using the NUSOAP library in my example. This will be an parameterized hello world web service. The client will call the web service and pass a name as a parameter to it. The result will be a
“Hello” greeting to the name given.
We need to do two things, (1) Setup the server side and (2) Setup the client side, from which we will call our web service.
DISCLAIMER: It is assumed that the reader knows basics of PHP development and can work on web servers or locally setup development servers.
Setting Up The Server Side
- Check SOAP is enabled on the server: Your server phpinfo() should look like this.If your phpinfo() does not look like the screenshot above, you need to locate your php.ini file and uncomment the semi-colon in the lines containing the soap DLL file (php_soap.dll in Windows). Save the modified php.ini and restart Apache Server. Then check your phpinfo() again.
- Create a new project folder on server.
- Install the PHP NUSOAP library: Download it from here.
- Extract the archive and keep the lib folder in your project folder.
- Call lib/nusoap.php in your PHP file where you will be working.
<?php require('lib/nusoap.php'); //make sure that the nusoap.php file is found at this path relative to your php source file ?>
- Create a SOAP server object.
<?php $soapServer = new soap_server(); //create the soap server object ?>
- Configure WSDL
<?php $soapServer->configureWSDL("HelloWorldWSDL", "http://www.helloworldsite.com"); //configure the WSDL ?>
- Register a method with your SOAP server.
<?php $soapServer->register('helloWorldWebSvc', array('name'=>'xsd:string'),array('return'=>"xsd:string"), "http://www.helloworldsite.com", "http://www.helloworldsite.com#helloWorldWebSvc", 'rpc', 'encoded', 'Hello World App!'); //register the method with the server ?>
- Make sure you call the service method of the SOAP server.
<?php /*$soapServer->service($HTTP_RAW_POST_DATA); //Fault:SOAP-ENV:Client error in msg parsing: xml was empty, didn't parse! */ @$soapServer->service(file_get_contents("php://input")); //FIXED ?>
Putting It All Together
<?php require("lib/nusoap.php"); $soapServer = new soap_server(); //create the soap server object $soapServer->configureWSDL("HelloWorldWSDL", "http://www.helloworldsite.com"); //configure the WSDL $soapServer->register('helloWorldWebSvc', array('name'=&gt;'xsd:string'),array('return'=&gt;"xsd:string"), "http://www.helloworldsite.com", "http://www.helloworldsite.com#helloWorldWebSvc", 'rpc', 'encoded', 'Hello World App!'); //register the method with the server function helloWorldWebSvc($name) //our function with business logic. Also called the server-side method. { return "Hello $name!"; } $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)?$HTTP_RAW_POST_DATA:''; /*$soapServer->service($HTTP_RAW_POST_DATA); //Fault:SOAP-ENV:Client error in msg parsing: xml was empty, didn't parse! */ @$soapServer->service(file_get_contents("php://input")); //FIXED exit; ?>
Important Note
It is important to note that there are outdated code snippets out there using $soapServer->service($HTTP_RAW_POST_DATA);
This wont work with NUSOAP in PHP 7. It will give the error:
Fault:SOAP-ENV:Client error in msg parsing: xml was empty, didn’t parse!
Fix it by using the following:
@$soapServer->service(file_get_contents(“php://input”));
Setting Up the Client Side
- Check SOAP is enabled on the server: This step is identical to its counterpart in the server-side above.
- Create a new project folder on server: This step is identical to its counterpart in the server-side above.
- Install the PHP NUSOAP library: This step is identical to its counterpart in the server-side above.
- Call lib/nusoap.php in your PHP file where you will be working: This step is identical to its counterpart in the server-side above.
- Create a SOAP client object.
<?php $client = new nusoap_client("http://localhost/HelloSvc/hello_world_web_svc.php?wsdl"); ?>
- Call the server-side method.
<?php $response = $client->call("helloWorldWebSvc", array("Some User")); //calling the registered method of the web service from client. ?>
Putting It All Together
<?php require("lib/nusoap.php"); $client = new nusoap_client("http://localhost/HelloSvc/hello_world_web_svc.php?wsdl"); $response = $client->call("helloWorldWebSvc", array("Some User")); //calling the registered method of the web service from client if($client->fault) { echo "Fault:".$client->faultcode." ".$client->faultstring; } else { print_r($response); } exit; ?>
Note: In Android and iOS apps, no PHP client is used generally. Instead coding is done in Java or Objective-C, etc that uses the HTTP Client API of the respective programming languages.
The End Result
If all goes well you will see a screen like the one below when you invoke the PHP Web Service client in your browser.
Note: This example is compliant with PHP 7.
I think this is one of the most significant info for me.
And i’m glad reading your article. Butt waqnna remark on few general things, The site style is
ideal, the articles is really excellent : D. Good job, cheers
Thank you! 🙂
That is a really good tip especially to those fresh to the blogosphere.
Brief but very accurate info… Appreciate your sharing
this one. A must read article!
Glad You liked it!
Right here is the perfect website for everyone who would like to understand this topic.
You know so much its almost tough to argue with you (not that I actually would
want to…HaHa). You definitely put a new spin on a subject that has been discussed for
decades. Wonderful stuff, just excellent!
Glad you liked it! Hope more and more people find it helpful!
I don’t even know the way I ended up here, but I thought this
put up was once great. I do not realize who you’re but certainly you are going to a famous blogger in the event you aren’t already
😉 Cheers!
I am extremely inspired together with your writing skills as
well as with the structure on your blog. Is that this a paid theme or did you customize
it your self? Anyway stay up the excellent high quality writing, it is uncommon to look a
nice weblog like this one today.
Nice post. I was checking constantly this blog and I am impressed! Very useful info specifically the last part 🙂 I care for such info a lot. I was looking for this particular info for a long time. Thank you and good luck.
Everything is very open with a really clear description of the issues.
It was definitely informative. Your site is very useful.
Many thanks for sharing!
Thanks for any other informative blog. The
place else could I get that type of info written in such a perfect approach?
I’ve a mission that I am simply now operating on, and I’ve been at the look out for such info.
I got this website from my buddy who told me concerning this site and now this time I am visiting this web site and reading
very informative content at this time.
You ought to be a part of a contest for one of the highest quality websites online.
I am going to recommend this web site!
Thanks for your appreciation! 🙂
I am not sure where you are getting your info, but great topic.
I needs to spend some time learning much more or understanding more.
Thanks for fantastic info I was looking for this information for my mission.
Long time reader, first time commenter — so,
thought I’d drop a comment.. — and at the same time ask for a favor.
Your wordpress site is very simplistic – hope you don’t mind me asking
what theme you’re using? (and don’t mind if I steal it?
:P)
I just launched my small businesses site –also built in wordpress
like yours– but the theme slows (!) the site down quite a bit.
In case you have a minute, you can find it by searching for “royal cbd” on Google
(would appreciate any feedback)
Keep up the good work– and take care of yourself during the coronavirus scare!
~Justin
Long time supporter, and thought I’d drop a comment.
Your wordpress site is very sleek – hope you don’t mind me asking what theme you’re using?
(and don’t mind if I steal it? :P)
I just launched my site –also built in wordpress like yours– but the theme
slows (!) the site down quite a bit.
In case you have a minute, you can find it by searching for “royal cbd” on Google (would appreciate any feedback) – it’s still in the works.
Keep up the good work– and hope you all take care of yourself
during the coronavirus scare!
Hello there! This blog post couldn’t be written much better!
Looking at this post reminds me of my previous roommate!
He always kept talking about this. I’ll forward this
information to him. Pretty sure he’ll have a great read. I appreciate you for sharing!
Nice post. I learn something totally new and challenging on websites
I stumbleupon every day. It’s always helpful to read articles from other authors and practice a little something from other sites.
We are a group of volunteers and starting a new scheme in our community. Your website offered us with valuable info to work on. You have done an impressive job and our whole community will be grateful to you.
I was pretty pleased to find this site. I wanted to thank you for your time just for this fantastic read!! I definitely savored every little bit of it and i also have you saved to fav to check out new things in your site.
Have you ever considered writing an e-book or guest authoring on other blogs?
I have a blog based on the same information you discuss and would love to have
you share some stories/information. I know my readers would appreciate your work.
If you are even remotely interested, feel free to shoot me an e mail.
My blog; Seattle Weekly (http://www.seattleweekly.com)
Good information. Lucky me I discovered your website by chance (stumbleupon). I have saved as a favorite for later!
Remarkable issues here. I am very happy to peer your post. Thanks a lot and I am having a look ahead to contact you. Will you kindly drop me a mail?
Hello! Someone in my Myspace group shared this site with us so I came to check it out. I’m definitely loving the information. I’m bookmarking and will be tweeting this to my followers! Great blog and outstanding design.
You actually make it appear really easy together with your presentation but I in finding this topic to be really one thing which I feel I would never understand. It seems too complicated and extremely extensive for me. I am taking a look forward to your next publish, I will attempt to get the dangle of it!
I do not even know how I ended up here, but I thought this post was good.
I don’t know who you are but certainly you’re going to a famous blogger if
you aren’t already 😉 Cheers!
Here is my site – buy Instagram followers (https://radaronline.com/)
It is remarkable, very amusing opinion