Topic 6. Writing Web Service Clients

Exercise

Select either SimpleXML or SAX to do these exercises, depending on your confidence in programming. As a general guideline I would only recommend SAX if you are thoroughly confident in programming. However, if you have a good understanding of the SAX code above, then by all means use SAX whatever the case.

The idea of this exercise is to write a remote website, IndieWorld, which specialises in indie/alternative music. IndieWorld wants to offer its users the opportunity to view all the songs released by its artists and their chart positions. However, rather than providing this information itself, it wishes to reuse the web service provided by HitTastic! (which you wrote earlier)

At the moment IndieWorld has pages on Oasis, Radiohead and the Killers. A user of IndieWorld should be able to select one of these three artists and view all the singles released by that artist.

Part 1 - Requesting the data using cURL

Write a script which reads in the user's chosen artist from a form (the form to use is shown below), and sends a cURL request to your HitTastic! web service to search for all hits by that artist. Try it out and simply display the XML returned from the cURL call, with:

echo htmlentities($response);
htmlentities encodes the < and > characters so that you can see them in the browser. Later you will actually parse, or interpret, the XML.

When you are done, upload the code to the IndieWorld folder on Edward. Note that in the real world, IndieWorld would be on a completely different location to HitTastic!, as they are different companies, but here, because we only have access to one server, the web service client and the web service will be on the same machine. However, to try and simulate a different server the best we can, you will upload your IndieWorld script to a different part of the Edward server.

You should login as the 'indieworld' user rather than your normal user. Log in using the 'indieworld' username and password that I give you in class. Make sure you create your own folder within the IndieWorld folder so you don't overwrite anyone else's work!

To access your IndieWorld code go to the URL:

http://edward/IndieWorld/(your username)/(IndieWorld form)

Here is the form you can use for IndieWorld.

<html>
<head>
<style type='text/css'>
body { background-color: black; color: white }
</style>
</head>
<body>
<h1>IndieWorld - your top indie music site</h1>
<form method="post" action="wsclient.php">
Artist:
<select name="artist">
<option>Oasis</option>
<option>Radiohead</option>
<option>Killers</option>
</select>
<input type='submit' value='Go!' />
</form>
</body>
</html>

Part 2 - Parsing the XML returned

Use SimpleXML or SAX to parse the XML, using the example above as a guideline. If you are using SimpleXML, note that you can use simplexml_load_string() rather than simplexml_load_file() to load XML in from a string (e.g. from the web), rather than from a file.

Part 3 - Buy script

Advanced

One problem with AJAX is that an AJAX client can only talk to a server side script on the same server. This is for security reasons: if an AJAX script could talk to other servers, they would be vulnerable to security exploits. However, you can get round that with a proxy script. If IndieWorld wanted to develop an AJAX interface and still talk to HitTastic!, they could write a PHP script which reads in information from the AJAX interface, forwards it on to HitTastic!, receives the XML returned from HitTastic! and sends it on to the AJAX interface.
Change the IndieWorld form so that it uses AJAX, and communicates with a proxy script. The proxy script should talk to the web service and receive the XML back. The XML should then be sent on to the AJAX interface to parse.