A real-life example:
all weather forecast services have nice looking, public websites where you can search for your own location, see the daily and weekly forecast and get various information.
You have probably noticed that even if there are thousands of weather forecast websites out there, many of them provide the same exact information.
That’s not a coincidence:
only a few companies have the expensive infrastructure to gather all the weather-related data required for forecast. Most of the weather websites are just reading the forecast data from those companies and showing it on their own websites.
Very often, this data exchange is done through web services.
The client sends an HTTP request to the provider’s service and gets some data in return.
Since this data is not meant to be readable by humans but to be easily handled by scripts and applications, it wouldn’t make any sense to use any HTML or CSS formatting for this data, right?
(In fact, parsing an HTML document programmatically can be quite difficult!)
Instead, web services use data-exchange protocols like:
- CSV: a very simple list of elements separated by a marker, like “data1;data2;data3;…”
This format is used for very simple services only.
- XML: a complex and powerful encoding language, suitable for very complex services.
- JSON: a modern encoding format very used today, simpler than XML and well supported by modern frameworks and libraries.
Writing a web service with PHP is easier than you think. In fact, support for both XML and JSON formats is already included in the standard PHP library.
See my XML and JSON tutorials to find some example code: