Website:
1. Response will be in HTML
2. Accessed by Humans via web browsers
WebService:
1. Accessed by Programs
2. Response will be in XML or JSON.
3. User to Communicate between heterogeneous Systems and Platforms
example :
getting response = http://localhost/index.php?id=x
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 |
<?php header("Content-Type:application/json"); function get($find){ $students = array( 1 => "male", 2 => "female", 3 => "shemale" ); foreach ($students as $student=>$name) { if ($student == $find) { return $name; break; } } } if (!empty($_GET['id'])) { $id = $_GET['id']; $name = get($id); if (empty($name)) { response(100, "Not Found", NULL); } else { response(200, "Student Found", $name); } } else { response(300, "Invalid Request", NULL); } function response($status, $student, $gender) { header("HTTP/1.1 $gender $student"); $res['gender'] = $gender; $res['student'] = $student; $res['status'] = $status; $json = json_encode($res); echo $json; } ?> |
change URL to get response jSON
create .htaccess
1 2 3 |
Options +FollowSymlinks RewriteEngine on RewriteRule ^([a-zA-Z_-]*)$ index.php?id=$1 [nc,qsa] |
http://localhost/index.php?id=x
modified
http://localhost/x