I’m starting a new “segment” called “Buzzwords that are actually really easy to implement”. There’s a LOT of misconceptions out there about stupid buzzwords that mean absolutely nothing, or sound super complicated but are actually very simple to understand. I’m getting pretty sick of seeing agencies throwing themselves out there and saying “WE DO HTML5, WE DO CSS3, WE DO DATABASE MANAGEMENT” when really what they mean is they throw on HTML5 Boilerplate or html5shiv and call it a day.

So I’m going to try and explain what these buzzwords ACTUALLY mean, and I’m started with “Web Services”

Web Services can be dumbed down to creating an API that outputs data from a database in a variety of formats (usually XML or JSON). For example, if you go the URL https://graph.facebook.com/508077801/, you would see a JSON output of my public Facebook data. It’s doing this by  looking up the ID I gave it. In this case, it’s 508077801. If we had a Facebook access token, we could do all sorts of things like get my entire Feed in JSON format (for example this: https://graph.facebook.com/508077801/feed?access_token={YOUR ACCESS TOKEN}), get my friend list, my photos, and a lot of other stuff.

So let’s write a similar (simplified) service that does more or less the exact same thing. We’ll write a small DB that contains the data, and output different results depending on the URL in JSON.

First what we’re going to do is create a table with a few rows of data, you can use this query.

CREATE TABLE tempdata (id int PRIMARY KEY NOT NULL AUTO_INCREMENT, firstname varchar(50), lastname varchar(50)); 
INSERT INTO tempdata (firstname,lastname) VALUES ('John','Doe');
INSERT INTO tempdata (firstname,lastname) VALUES ('Greg','Reed');
INSERT INTO tempdata (firstname,lastname) VALUES ('Joe','Blow');

This creates 3 rows with 3 different names. Next we’ll write a small PHP script that will bring back the results depending on what number we pass in the URL, just like Facebook does (only this way isn’t as clean).

$dbHost = "HOST";        //Location Of Database usually its localhost
$dbUser = "USER";            //Database User Name
$dbPass = "PASS";            //Database Password
$dbDatabase = "DATABASE";       //Database Name
$db = mysql_connect($dbHost, $dbUser, $dbPass) or die ("Error connecting to database.");
mysql_select_db($dbDatabase, $db) or die ("Couldn't select the database.");
$id = $_GET['id'];
$query = mysql_query("SELECT firstname, lastname FROM tempdata WHERE id = ".$id) or die(mysql_error());
$row = mysql_fetch_array($query);
$array = array('firstname'=>$row['firstname'],'lastname'=>$row['lastname']);
echo json_encode($array);

What we’re doing is querying for the entries based on the GET variable, and passing that into the query (an unescaped query in my example I should add). Then we pass the results into an array, to be echoed out as JSON. So if your URL looks like this “http://localhost/webserviceexample.php?id=1”, you will return this output (or something similar).

firstname: "John",
lastname: "Doe"

We could add more vars to the mix, but this shows you exactly what a web service actually does. Now anyone that has access to that URL can use that data from our database in their own scripts and applications. What it would be generally used for public data that anyone could access securely/safely. We could add public API keys or hashes to secure it to only a few users, or we could add more to the database/query to get even more data.

Anyways, this is what a “web service” is, and (to me at least) it’s very simple to implement for any project requiring it.

2 comments so far

Add Your Comment
  1. This is a topic that’s near to my heart… Cheers! Where
    are your contact details though?