There are several explanations on the internet, I will try to respond in a less technical way and show when to use, because we learn faster with the need. So first I will impose ways to need certain features but that are not the best ways to do it and by necessity you need a Web service.
Web service
When to use?
I want to create a news website on the internet, my target audience are Android users, iOS and website. My database needs to be unique and shared with these three platforms.
Initially you would think I can make Android communicate directly with the database and fetch the necessary information, so it would have to create search, insert, and delete functions.
For iOS I would also need these functions I could communicate direct too, and my website also needs that.
Interesting each platform would have its way of communicating with the database.
Why not do this?
Most importantly, security, you can not communicate directly with a database, it would be an unnecessary risk.
Another point to take into consideration is that if you noticed all have common functions, everyone searches for the same data, makes the same types of inserts and deletes in the database, so why can not I create a " face "that is on the side of the database, this guy does all the communications for you with the database, Android iOS do not request the same thing? so I only do a given function once and make it available at an internet address, example
www.meuwebservice.com/pegar_todos_os_posts_do_dia.php
. Now when Android and iOS want to pick up the "news posts" from the database it asks the Web service for a service. The Web service only provides the posts, for it does not matter who requested it if it is Android, iOS, website, desktop application.
In order to finalize on Web service, it is nothing more than a program that offers services via internet, for different platforms regardless of the programming language used, it shares resources between different platforms.
SOAP and REST
But wait, the language of Android is in JAVA, iOS is in Objective-C and my website is in Python, and my Web service is still in PHP. One speaks English, another Portuguese, and another Chinese. A mixture of languages. How can I solve this? well, we need an intermediate language that everyone should understand. But for this we need to define a protocol (standard) of communication.
There you enter the SOAP or REST communication protocols.
SOAP is the oldest, is a message transfer protocol in the form XML . The messages that will be exchanged have a very strict and XML-bound pattern.
REST is a more flexible protocol, you do not get tied to XML, you can create pure text message, message formatted JSON or even XML.
So how does this communication work?
Android makes a request for a service to the Web service, it wants all the news posts of the day, the web service, then retrieves the posts of the day, turns the set of posts into an XML file, and sends that file to Android XML, Android receives the XML file, it knows how to read the XML, then it reads the data inside the XML and converts the data to handle in its native language. IOS also wants the same data type, requests the Web service, the Web service sends the same XML, iOS also know how to read XML, then it decodes it to handle in its native language. The same thing for a website, or even a desktop application made in Delphi, everyone gets the same XML, everyone knows how to read the XML, and convert it to their native language, and the Web service performs a single function for all platforms.
Remember that they only know how to read the XML because there is a standard when the XML was created, for example, within the body of the XML file, there should be a piece that refers to the context of the communication, this part must be at the beginning of the file and calls header, and a part of the file must contain the data that the client requested, and will be called body. This applies to SOAP where the file has a hard standard when it is created.
More about Web service .
More about SOAP and REST .