Recently I was able to implement my first web service. I implemented this in php and it is a very primitive service. The fundamental idea of implementing this was to get the first hands on experience in web services. To benefit those who are like me I would like to share my project here.
What is a Web Service?
To be frank this is a big question that I had in my mind, which I recently found the answer. To be precise, web services are software components that can be run over the internet where those components use XML to communicate with each other (which are technically called remote procedure calls).
The main advantage of a web service is that it enables applications to be built which are platform independent and distributed.
What is SOAP?
SOAP stands for Simple Object Access Protocol. It specifies how the software components should request and receive messages within each other. SOAP messages are remote procedure calls that consist of XML which are send over HTTP. Therefore the differences between programming languages within software components does not hinders the communication between them and it allows seamless integration between them.
Setting up the environment
For this project I used xampp server stack on linux as my server. You can use xampp for linux, wampp for windows or any other alternative server. But make sure that you have enable SOAP in your web server.
Implementing the SOAP server
The next thing I did was implementing the SOAP server. But before that there is some other thing that has to be done. That is setting up a database. In my project I implemented a simple web service which provide census data in various districts in Sri Lanka. First this data has to be come from a database. For this I used MySQL database in xampp.
First create a database (eg. census) .Then you need to create your database table. I implemented a table with 4 columns, first column is the district name, second, third, and fourth columns are for the population value in each district in year 1981, 2001, and 2012 respectively.<script src="https://gist.github.com/3115704.js?file=census_db.sql"></script>Then you need to insert data in to the table.<script src="https://gist.github.com/3115710.js?file=census_table.sql"></script>
Now the database is ready for use.
The SOAP server that I implemented had only one method, get_population($district,). The SOAP server will return all the population values for that particular district in years 1981, 2001 and 2012. The following is the census_server.php code.
<script src="https://gist.github.com/3115687.js?file=census_server.php"></script> Now the SOAP server is ready for action. The next task is to implement the SOAP client.
Implementing the SOAP client.
I implemented a simple SOAP client in php. The following is the code for it. The content is self explanatory. <script src="https://gist.github.com/3115980.js?file=client.php"></script>
Implementing a web page for the SOAP client
Now the web service components are almost completed. But we need to provide an interface to the SOAP client. The following code implements the web interface for the client.<script src="https://gist.github.com/3116054.js?file=gistfile1.txt"></script>
Now everything required for the web service to work is implemented. The bellow picture shows my web service in action.