Creating a JSON Web Service in PHP
Development, PHP July 4th, 2008When using alot of Ajax it is recommended to use JSON instead of XML for data. This is mainly because JSON is native to Javascript and is faster to process. As Script.aculo.us creator Thomas Fuchs puts it XML is “dog slow…don’t do it”. In this tutorial i’m going to show you a simple implementation of a PHP JSON web service.
This web service I use for a lot for the Yahoo UI Library, especially the Data Table.
Introduction
Before we begin, this example requires > PHP 5.2 to encode the JSON format. This is because i’m using the json_encode function in PHP. A simple example on how to use this function is here:
1 2 3 4 5 6 | <?php $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); echo json_encode($arr); // {"a":1,"b":2,"c":3,"d":4,"e":5} ?> |
Implementation
The way I have implemented this web service is by passing a parameter called action into my json_webservice.php file. These actions I simply compare using a switch statement and then return the JSON I want based on the action.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <? // json_web_service.php // connect to mysql database $conn = mysql_connect(HOST, USERNAME, PASSWORD) or die('Error connecting to mysql'); mysql_select_db(DB); // get some parameters $action = $_REQUEST['action']; $condition = $_REQUEST['condition']; // Array used to encode the JSON $arr = array(); switch($action) { case "get_artists": $result = mysql_query("SELECT * FROM artists"); while($art = mysql_fetch_object($result) { $arr[] = $art; } echo json_encode($arr); break; case "get_albums_by_artist_id": $result = mysql_query("SELECT * FROM albums WHERE album.artist_id = $condition"); while($art = mysql_fetch_object($result) { $arr[] = $art; } echo json_encode($arr); break; case "get_album_by_name": $result = mysql_query("SELECT * FROM albums WHERE album.artist_id = $condition"); while($art = mysql_fetch_object($result) { $arr[] = $art; } echo json_encode($arr); break; } ?> |
You can add security to your Json web service by simply doing some checks for session variables as you would for normal PHP files.
Usage
Using Javascript and a Json Library you can use the Json service above, by using the URL with the parameters you need.
1 2 3 4 5 6 7 | // json_web_service.php?action=get_artists [{"id":"123", "name": "Snoop Dog"}, {"id":"125", "name": "Oasis"}] // json_web_service.php?action=get_albums_by_artist_id&condition=123 [{"id":"1", "name": "Snoop Dog Best Hits"}, {"id":"2", "name": "Doggy Diggy vol. 1"}] |
You might wanna start looking into creating some Ajax now. Have a look at Yahoo UI Library for some cool things you can do with Json.
July 14th, 2008 at 11:52 am
Seems like there are a couple typos in your code: e.g. mysql( should be mysql_query(
January 7th, 2009 at 12:58 am
A well written Article indeed,
I loved its clear to the point style…
I wont argue with you that JSON is better suited for AJAX than XML, but what about returning HTML! Yes its not technically AJAX if we return HTML, but I was wondering about adding that to your article.
I have met some people that claim to be able to developed AJAX applications when they don’t know what JSON is… “No offense meant to anyone”
I just wish someone would write a basic to the point article about those little, huge miss understandings!
All the best regards and wishes,
Tamer Qtaish