This exercise will allow you to develop a REST-compliant web service, and also develop a client to connect to the service.
<?php
$a = $_GET["id"];
$conn = mysql_connect("localhost","dftitutorials", "dftitutorials");
mysql_select_db ("dftitutorials");
if($a<=0)
{
// What HTTP code should we return if $a is invalid (0 or less)?
exit; // quit on error
}
// Check that the ID exists
$result=mysql_query("select * from hits where ID=$a");
if(mysql_num_rows($result)==0)
{
// What HTTP code should we return if the ID doesn't exist?
exit; // quit on error
}
if ($_SERVER["REQUEST_METHOD"]=="GET")
{
// retrieve the song
}
elseif ($_SERVER["REQUEST_METHOD"]=="DELETE")
{
// delete the song
}
elseif ($_SERVER["REQUEST_METHOD"]=="PUT")
{
// update the song
}
mysql_close($conn);
?>
header("HTTP/1.1 200 OK");
You will want to substitute 200 OK with an appropriate error code.
http://edward/students/pjones/restserver.php?id=1009(note the use of /students/pjones rather than ~pjones. They link to the same folder. The reason why you should do this is that there are difficulties with using mod_rewrite with the ~username syntax for user directories).
Having set up your web service, have a go at setting up a REST style URL for it. Rather than something like
http://edward/students/pjones/restserver.php?id=1009the URL should read something like:
http://edward/students/pjones/hit/1009
Write a .htaccess file to do this, as described in the notes, and upload it to your public_html directory on the Edward server.
Web services would normally be called from a client script or application, for example with cURL. The only problem is that using unusual HTTP methods, such as PUT or DELETE, from cURL is long-winded. Because of this, I have provided a PHP script which makes calling a REST Web Service easier, saving you from having to use cURL directly, as the code to handle the different types of request (GET, PUT, DELETE, POST) can get very complicated! (lack of client support for PUT and DELETE is an important disadvantage of developing REST web services). You can download it here (ZIP file). To use it in a script, you need to add the line:
include('call_web_service.php');
at the top of your script.
With this script, to call any Web Service all you need to do is:
$result = call_web_service( URL, method, data);
http://hittastic.com/song/1009you would send some XML to update the details of song 1009. (you only need to specify data in the case of PUT or POST)
$result = call_web_service
("http://edward.solent.ac.uk/students/pjones/hit/1009",
"GET");
would send a GET request to the specified URL, causing the data for hit 1009
to be retrieved, and
$result = call_web_service
("http://edward.solent.ac.uk/students/pjones/hit/1009",
"DELETE");
would send a DELETE request to the URL, causing hit 1009 to be deleted.
$result contains the result returned from the web service. It contains two fields:
<html>
<head>
<style type='text/css'>
body { background-color: black; color: white }
</style>
</head>
<body>
<h1>Welcome to the IndieWorld Test REST client</h1>
<p>Not very user-friendly, for test purposes only. When you write your
assignment, make sure it's more user-friendly than this; obviously your average
web user is not going to know what an "HTTP request" is!!!! </p>
<form method="post" action="rest_client.php">
ID of song:
<input name="id" />
<br />
HTTP Request Type:
<select name="requesttype">
<option>GET</option>
<option>DELETE</option>
<option>PUT</option>
</select>
<br />
XML Data to Send (PUT only):<br />
<textarea name="data"></textarea> <br />
<input type='submit' value='Go!' />
</form>
</body>
</html>
if ($response["code"]==401) // 401 Unauthorized
{
echo "ERROR: You don't have permission to use the web service in this way";
}
Only do this if you are comfortable with everything we've done so far.
Have a go at implementing a PUT request for your web service. Just for the purposes of testing (obviously in the real world it would not be done this way!) add an extra field to your form, allowing the user to enter XML of the updated details of the song. The IndieWorld script should send a PUT request to the web service, and send the XML the user entered, in the same format as that returned from the GET request.
You then need to modify the web service to parse the incoming XML. Use SAX or SimpleXML (Topic 6) to parse the incoming XML and then perform an SQL UPDATE statement using the information extracted from the XML.
In your web service, you'll need to use this code to read the data sent using PUT:
// Read the XML data sent using the PUT request
// into the variable $putData using this line...
$putData= file_get_contents('php://input');
// Now parse the XML (see topic 6) and update the database...