Question : How to write an Web Service API ?


Hello group,

I'm about to write a Web App but at the same time I need to embed (if I'm right) a Web Service in it so that I can send and receive information to front-end through XML.

Where from can I start learning this? Any online tutorial or sample that I can learn from?

Thanks,
ak

Answer : How to write an Web Service API ?

A really good example of a RESTful API is the Yahoo Geocoder.
http://developer.yahoo.com/maps/rest/V1/geocode.html

The RESTful design pattern places all of the arguments in the URL string.  You see this sort of thing all the time in web pages, and in fact the entire WWW is a RESTful design.  Your web service script takes the information in the $_GET array and uses it to create the response, which can be XML, JSON, CSV, plain text, etc.  

Each REST call is atomic - there are no login/logout sequences.  You might choose HTTPS and some kind of API-Key authentication if you really wanted to.

Try these URLs to see how this works.  Note that testing the RESTful web service is a simple as typing the arguments into the browser address bar.

http://www.laprbass.com/RAY_REST_get_last_name.php
http://www.laprbass.com/RAY_REST_get_last_name.php?key=ABC
http://www.laprbass.com/RAY_REST_get_last_name.php?key=ABC&name=Fred
http://www.laprbass.com/RAY_REST_get_last_name.php?key=ABC&name=Richard

Without too much effort you can transform this design to use different API keys for different client data models, to return an XML string, or look up information in a data base, etc.

Best regards, ~Ray
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:
<?php // RAY_REST_get_last_name.php
error_reporting(E_ALL);


// DEMONSTRATE HOW A RESTFUL WEB SERVICE WORKS
// CALLING EXAMPLE:
// file_get_contents('http://laprbass.com/RAY_REST_get_last_name.php?key=ABC&name=Ray');


// OUR 'DATA MODEL'
$dataModel = array
( 'Brian'   => 'Portlock'
, 'Ray'     => 'Paseur'
, 'Richard' => 'Quadling'
)
;

// TEST THE API KEY
$key = FALSE;
if (isset($_GET["key"])) $key = $_GET["key"];
if ($key !== 'ABC') die('BOGUS API KEY');

// LOOK UP THE LAST NAME
$name="?";
if (isset($_GET["name"])) $name = $_GET["name"];
if (array_key_exists($name, $dataModel))
{
    die("$dataModel[$name]");
}
else die('UNKNOWN');
Random Solutions  
 
programming4us programming4us