Monday, 25 February 2013

Example of JSON server / javascript client

Here is the code from the lecture last week that shows how to consume a  JSON web service using JavaScript (JQuery)

First the client, then the server PHP below



<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML5 Template</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<style>
#output div{display:none;}
</style>
</head>
<body>
<script>
var url = "http://www.cems.uwe.ac.uk/~pmatthew/ATWD/jsonxml/index.php";
$(document).ready(function(){
$.ajax(url)
.done(function(data) {
var dataparsed = JSON.parse(data);
//console.log(dataparsed);
for(var i=0; i<dataparsed.data.value.length; i++){
$("#output").append("<div id='" + dataparsed.data.value[i].id + "'>"+ dataparsed.data.value[i].name + "</div>");
$("#" +(i+1)).fadeIn(i * 1200);
}
})
.fail(function() { alert("error"); }) });
</script>
<div id="output"></div>
</body>
</html>
view raw client.html hosted with ❤ by GitHub
<?php
//define some XML
$xml = <<<EOT
<root>
<data>
<value>
<id>1</id>
<name>Fred</name>
</value>
<value>
<id>2</id>
<name>Wilma</name>
</value>
<value>
<id>3</id>
<name>Barney</name>
</value>
<value>
<id>4</id>
<name>Betty</name>
</value>
</data>
</root>
EOT;
//create an object
$xmlobj = new SimpleXmlElement($xml);
//var_dump($xmlobj);
//now convert object to JSON
echo json_encode($xmlobj);
?>
view raw server.php hosted with ❤ by GitHub

Friday, 15 February 2013

.htacess file and xml reader example

.htaccess example:

RewriteEngine On # Turn on the rewriting engine
RewriteRule ^atwd/books/(course|detail|suggestions)/([0-9]+)/(xml|json)$ http://www.cems.uwe.ac.uk/~p-chatterjee/atwd_assignment_2013/index.php?type=$1&id=$2&format=$3 [L]
RewriteRule ^atwd/books/.*$ http://www.cems.uwe.ac.uk/~p-chatterjee/atwd_assignment_2013/error.php [L]

xml reader code and xml file

<?php
$reader = new XMLReader();
$reader->open('../xml/quotes.xml');
while ($reader->read()) {
if ($reader->nodeType == XMLREADER::ELEMENT) {
if ($reader->localName=='quote') {
$category[] = $reader->getAttribute("category");
}
if ($reader->localName=='text') {
$reader->read();
$text[] = $reader->value;
}
if ($reader->localName=='name') {
$reader->read();
$name[] = $reader->value;
}
if ($reader->localName=='dob') {
$reader->read();
$dob[] = $reader->value;
}
if ($reader->localName=='dod') {
$reader->read();
$dod[] = $reader->value;
}
if ($reader->localName=='url') {
$reader->read();
$url[] = $reader->value;
}
if ($reader->localName=='img') {
$reader->read();
$img[] = $reader->value;
}
}
}
$count = count($category);
for ($i=0; $i<$count; $i++) {
echo '<table>';
echo '<tr>';
echo '<td>Category</td>';
echo '<td>'.ucfirst($category[$i]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td>Quote</td>';
echo '<td>'.$text[$i].'</td>';
echo '</tr>';
echo '<td>Author</td>';
echo '<td><a href="'.$url[$i].'">'.$name[$i].'</a></td>';
echo '</tr>';
echo '<td>Date</td>';
echo '<td>'.$dob[$i].'-'.$dod[$i].'</td>';
echo '</tr>';
echo '<td>Image</td>';
echo '<td><img src="'.$img[$i].'" width="200"/></td>';
echo '</tr>';
echo '</table>';
echo '<p/>';
}
?>
view raw gistfile1.php hosted with ❤ by GitHub
<?xml version="1.0" encoding="UTF-8"?>
<quotes>
<quote category='politics'>
<text>The great are only great because we are on our knees. Let us rise!</text>
<author>
<name>Pierre-Joseph Proudhon</name>
<dob>1809</dob>
<dod>1865</dod>
<url>http://en.wikipedia.org/wiki/Pierre-Joseph_Proudhon</url>
<img>http://upload.wikimedia.org/wikipedia/commons/e/ea/Portrait_of_Pierre_Joseph_Proudhon_1865.jpg</img>
</author>
</quote>
<quote category='romance'>
<text>Take away love and our earth is a tomb.</text>
<author>
<name>Robert Browning</name>
<dob>1812</dob>
<dod>1889</dod>
<url>http://en.wikipedia.org/wiki/Robert_Browning</url>
<img>http://upload.wikimedia.org/wikipedia/commons/4/49/Robert_Browning_1865.jpg</img>
</author>
</quote>
<quote category='humour'>
<text>It's not that I'm afraid to die, I just don't want to be there when it happens.</text>
<author>
<name>Woody Allen</name>
<dob>1935</dob>
<dod/>
<url>http://en.wikipedia.org/wiki/Woddy_Allen</url>
<img>http://upload.wikimedia.org/wikipedia/commons/0/0f/Woody_Allen_at_the_premiere_of_Whatever_Works.jpg</img>
</author>
</quote>
</quotes>
view raw gistfile1.xml hosted with ❤ by GitHub