TL; DR : Webservices allow two (or more) machines to communicate over a network.
For a deeper explanation, imagine the following scenario: Your company trades dollar-denominated products whose value fluctuates daily. It is extremely important that you have these values updated so that you make decisions that bring you the best results. You can hire a trainee to update these values manually, or you can consume a service to provide this information automatically and reliably.
This service is the same as webservice . You ask a question (how many five dollars are worth a dollar today?), And he responds. This process, at the communication level, occurs through requests based on the HTTP protocol . There is an endpoint on a server that is always waiting for the question, and if it is done correctly, it responds. In a quick search on google, I found a webservice that perfectly illustrates the case of dollar (and virtually all currencies) quotation, which can be accessed here .
The question (or request) must be made correctly, otherwise webservice will not be able to understand the request. In the case of the quote, the question should contain the two currencies you want to convert, right? Using the webservice linked above, I made the following request (in cool terms, I entered this site):
link
and the response was XML
in format
<double xmlns="http://www.webserviceX.NET/">3.9818</double>
That is, through a GET request (the parameters are explicit in the URL), I got the information I wanted within XML
. How did I know which parameters to use or, in other words, what is the correct format of the question? In this specific case, who provided the service also provided the parameters it is waiting for the desired information to be reached. Try the requisition
link
to see what happens.
The GET request is one of the four standard HTTP protocol requests . The others are POST , PUT and DELETE . I will not go into details of how they all work, but you can read about the HTTP protocol here .
XML
(which is the return format) is a markup language that allows you to manipulate the data (or data) with ease. In PHP
, the simplexml_load_file()
method allows you to read XML
, extract the data and do with them whatever you want. To make a .php
file talk to another, you could use the cURL (which is giving me a beating this week). There is also the endless Ajax , which allows you to consume services asynchronously. I particularly prefer to use the JSON
format for responses, because most applications that consume my services are written with AngularJS
.
There are some standardizations for services, in the sense of how questions and answers should be asked and constructed. At the webservices wiki , you can find a number of types of services that are based on markup languages.
You may also come across the term RESTful APIs, which are basically services that are based on the REST architecture . The services I build are always based on that model. There is also this great story that explains how that architecture works.
For the icing on the cake, and for the sake of curiosity, WordPress is deploying a RESTful API named WP API , which goes basically allow to make any instance of WP in a webservice. A hand in the wheel, in my opinion. =)