If you only ever expect GET requests, there's nothing wrong with what you have per se. Is there a better way? There are certainly different ways that may be deemed better depending on what you need to do. Since your trying to get the basic idea of web services, I'll address that.
There are a number of ways to implement web services, most of which turn out to be overkill for basic applications. I think the simplest to understand and implement is REST.
REST is an architecture style for designing networked applications.
The idea is that, rather than using complex mechanisms such as CORBA,
RPC or SOAP to connect between machines, simple HTTP is used to make
calls between machines. — Learn REST: A Tutorial
Also have a look at:
There are other methods (e.g. SOAP) that I have no experience with, so I can't speak intelligently about them or their merits.
The next thing to consider is your API. I understand the code in your question is meant to be an example, so I'll use it as such. What you have tells the client nothing about the data being returned. It could be an array or a string. Ideally, you would create some structure around the data being returned. For example, you might return an error flag that indicates to the client if an error occurred.
Your server code could be written as:
<?php
$data = new stdClass;
$data->error = false;
if ($_SERVER['REQUEST_METHOD'] != 'GET') {
$data->error = true;
$data->response = 'Invalid HTTP request';
}
if (!isset($_GET['word'])) {
$data->error = true;
$data->response = 'Invalid URL parameter';
}
if (!$data->error) {
if ($_GET['word'] == 'A') {
$data->response = array('aa', 'ab', 'ac', 'ad', 'ae');
} else {
$data->response = array('bb', 'bc', 'bd', 'be', 'bf');
}
}
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);
?>
Response with error
{
"error": true,
"response": "Invalid URL parameter"
}
Response without error
{
"error": false,
"response": [
"aa",
"ab",
"ac",
"ad",
"ae"
]
}
Now the client code can determine if an error occurred and what it was. With this simple API, I know that if an error occurred, the response will be a string describing the error. If there was no error, then I can assume the response will be an array. As for the actual data, I generally use objects or associative arrays so that each piece of datum has a label.
For more information please see: How to Design a Good API & Why it Matters (video lecture)
Ultimately, there are as many ways to build a web service as there are people to build them. The best way depends on your needs and how easy/hard it's going to be to upgrade and maintain it in the future.