Professional Documents
Culture Documents
Tutorial – 10
March 2022
ITAP3012 Tutorial 10
Individual Assessment:
Weightage: 10%
Report deadline: Week 11
LO6 Compose service-oriented applications using web services and standard communication
frameworks
Guidelines
You need to complete the following activities and put all your answer in the Tutorial 10 report (word/pdf)
file and submit on Moodle.
Activity 1
Create a XML file with the <Article> as a root tag. Each article contains the Headline, Author, Para and
each para there is an instrument. Fill all the details and save the file as .xml. Open in the browser and
take the screenshot and save
<ARTICLE>
<HEADLINE> ----------- </HEADLINE>
<AUTHOR>--------------- </AUTHOR>
<PARA>
------------------------------
<INSTRUMENT>------------</INSTRUMENT>
------------------------------
</PARA>
</ARTICLE>
Activity 2
Create a “common.css” file and add styles to your XML file created in Activity 1. You need to add the
following styles
1. The display property for INSTRUMENT is inline and for all other it should be block
2. The font-family for ARTICLE should be serif
3. The text of PARA should be indented 1em.
4. The font style of the INSTRUMENT should be italic.
You need to add the common.css file to your XML file using following code
<?xml-stylesheet href="common.css"?>
<ARTICLE>
-----------------------------------------
-----------------------------------------
</ARTICLE>
Activity 3
We have created an art.xml file that contain the paintings record and test.html file that Html/JavaScript
code to get the “ID” and “Title” of each painting. Read the code and write the comments for each
JavaScript line. You can use online resources.
//Art.xml //Test.html
<?xml version = "1.0" encoding = "ISO-
8859-1"?> <script>
<art> if(window.XMLHttpRequest){
<painting id="290"> var xmlhttp = new XMLHttpRequest();
<title>Balcony</title> }
<artist> else{
<name>Manet</name> var xmlhttp = new XMLHttpRequest();
<nationality>France</nationality> }
</artist>
<year>1868</year> xmlhttp.open("GeT","art.xml",false);
<medium>Oil on canvas</medium> xmlhttp.send();
</painting> var xmlDoc = xmlhttp.responseXML;
<painting id="192">
<title>The Kiss</title> var paintings =
<artist> xmlDoc.getElementsByTagName("painting");
<name>Klimt</name> if(paintings)
<nationality>Austria</nationality> {
Activity 4
Consider the following code that is used to read data from the “art.xml” file discussed in Activity 3. Please
read the code and modify it to a show each painting title, author name, and year
<?php
$filename = 'art.xml';
if(file_exists ($filename)){
$art = simplexml_load_file($filename);
$painting = $art->painting[0];
echo '<h2>'.$painting->title.'</h2>';
echo '<p> By'.$painting->artist->name.'</p>';
echo '<p> ID = '.$painting["id"].'</p>';
echo "<ul>";
foreach ($art->painting as $p) {
echo '<li>'.$p->title.'</li>';
}
echo '</ul>'; }
else{
exit('Failed to Open '.$filename); }
?>
Activity 5
Consider you have been given the following JSON string.
'{"artist": {"name":"Manet","nationality":"France"}}';
1. Write the PHP code to get the name and nationality of the artist?
2. Write the JavaScript code to get the name and nationality of the artist?
Activity 6
Create a simple REST API in PHP using the instructions given below. You need to complete this activity
and share the screenshots with brief explanation of your final product in the Tutorial 10 report file.
What is REST
REST stands for Representational State Transfer as discussed in the lecture. It is an architectural style
which defines a set of constraints for developing and consuming web services through standard protocol
(HTTP). REST API is a simple, easy to implement and stateless web service. REST can provide output data
in multiple formats such as JavaScript Object Notation (JSON), Extensible Markup Language (XML),
Command Separated Value (CSV) and many other formats.
To implement CRUD operations REST uses, GET, POST, PUT and delete request. GET is used to retrieve
information. POST is used to create a new record. PUT is used to update the existing record and DELETE
is used to delete an existing record.
The most common output format for REST API is JSON format.
In this activity you will develop a REST API for online transaction payments.
Create a Database
First create a database (make sure the database name is same as your name) and add one table in it called
transactions using the following code.
You also need to add some dummy data to your transaction table using INSET query.
Connect to a Database
Create a new db.php file and write a database connection code in the file. Use your database credentials
to connect with the database
$con = mysqli_connect("localhost","root","","yourname");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
Create another file called api.php and add the following script to it. You need to carefully read the
following scrip and identify the purpose of each statement.
<?php
header("Content-Type:application/json");
if (isset($_GET['order_id']) && $_GET['order_id']!="") {
include('db.php');
$order_id = $_GET['order_id'];
$result = mysqli_query(
$con,
"SELECT * FROM `transactions` WHERE order_id=$order_id");
if(mysqli_num_rows($result)>0){
$row = mysqli_fetch_array($result);
$amount = $row['amount'];
$response_code = $row['response_code'];
$response_desc = $row['response_desc'];
response($order_id, $amount, $response_code,$response_desc);
mysqli_close($con);
}else{
response(NULL, NULL, 200,"No Record Found");
}
}else{
response(NULL, NULL, 400,"Invalid Request");
}
function response($order_id,$amount,$response_code,$response_desc){
$response['order_id'] = $order_id;
$response['amount'] = $amount;
$response['response_code'] = $response_code;
$response['response_desc'] = $response_desc;
$json_response = json_encode($response);
echo $json_response;
}
?>
The above script accepts GET request and return the output in JSON format. As you put all your php files
in ITAP3012_tutorial10, you can get the transaction information using the following URL
http://localhost/ITAP3012_tutorial10/api.php?order_id=12345
To create a simple form, you can use the following HTML code
Fetch Records
To fetch the records for this tutorial, make sure CURL is enabled on your web server or on your localhost
when you are testing. You can use the following code to access the records
<?php
if (isset($_POST['order_id']) && $_POST['order_id']!="") {
$order_id = $_POST['order_id'];
$url = "http://localhost/rest/api?order_id=".$order_id;
$client = curl_init($url);
curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($client);
$result = json_decode($response);
echo "<table>";
echo "<tr><td>Order ID:</td><td>$result->order_id</td></tr>";
echo "<tr><td>Amount:</td><td>$result->amount</td></tr>";
echo "<tr><td>Response Code:</td><td>$result->response_code</td></tr>";
echo "<tr><td>Response Desc:</td><td>$result->response_desc</td></tr>";
echo "</table>";
}
?>
Reference
https://www.allphptricks.com/create-and-consume-simple-rest-api-in-php/