UFCEKG‐20‐2 Data, Schemas & Applications Lecture 4 Server Side Scripting & PHP Last week: o encode data for communication o card based o csv o tagged records o Xml o xml vocabularies o xml processing vocabularies o xml namespaces o xml characteristics o rss o rss aggregation

Feb 2013 N. H. N. D. de Silva 2 Server‐side languages

Processing carried out on a

o SSI ‐ Server‐Side Include with some processing o Perl o PHP o Python o ASP (C#, VB, F# ..) o JSP o Ruby o XSLT o XQuery o Javascript (with jNode)

Feb 2013 N. H. N. D. de Silva 3 Muliltip le languages

We will be studying a number of different languages on this module because

o Web development typically uses multiple languages o New langgguages are appearin g all the time o Language design is evolutionary o Comparing the syntax and features of several languages helps to understand computer languages in general

ege.g. How do different languages handle Strings

Feb 2013 N. H. N. D. de Silva 4 SSI (Server Side Includes) Processed by the web server File suffix ‐ shtml.. Include a common file

Execute a Unix command or cgi script and include the result

Feb 2013 N. H. N. D. de Silva 5 PHP Origins Originally created by Rasmus Lerdorf (born Greenland, educated in Canada) around 1995

PHP originally abbreviated as ‘Personal Home Pages’, now known as ‘PHP Hypertext Pre‐processor’

Other key developers: Zeev Surashi and Andi Gutmans (Israel) responsible for PHP 330.0 ‐ a complete re‐write of the original PHP/F1 engine (1997)

PHP is Open Source software First version PHP/FI released 1995 PHP 505.0 released July 2004 Current stable version is 5.4.6 (as of August 2012) PHP version 5.2.4 current at UWE PHP Version 6 due for release since late 2010 but ?? Feb 2013 N. H. N. D. de Silva 6 PHP ‘Hello World’ PHP is often used to generate HTML pages with dynamic content. Here it is used like SSI. The markup are one way to delimit PHP code within HTML.

Basic Pagg/e

Basic Page

HllHello wor ld/ld

This page is different every time it is loaded

Current date/time is

Run

Feb 2013 N. H. N. D. de Silva 7 Accessing URI parameters

o PHP uses C conventions for assignment, statement separators, blocks etc. o PHP provides the parameters from the URI as an array of values. $_REQUEST o arrays can be indexed by position or key value o Variables start with the character $ o To join (concatenate ) strings use the . (dot) operator

Run

Feb 2013 N. H. N. D. de Silva 8 Form interface A common approach would be to provide an HTML form to allow the user to enter the parameters Form

Form

Current date

Run

Notice that when the url is entered into the form, the browser url‐encodes the Feb 2013special characters. N. H. N. D. de Silva 9 Weather pipe RSS feed Replacement

This will be constructed along the same lines as a Yahoo pipe, with functions in place of modules

[Get data] >> [Parse CSV] >> [Compute new data] >> [create RSS]

Feb 2013 N. H. N. D. de Silva 10 Handling the proxy server

First we need to be able to fetch a data file. Because there is a proxy server at UWE and because we don't want it to cache the data (why?), we need a bit of magic. array('proxy'=>'proxysg.uwe.ac.uk:8080', 'header'=>'Cache-Control: no-cache' ) )); $contents = file_get_contents($uri,false,$context); ret$ttturn $contents; };

This ddfiefines a ffiunction whic h has one parameter, a uri and returns the file contents. This is reusable for any file we want to get via HTTP running on a BIT server. Feb 2013 N. H. N. D. de Silva 11 Arrays The index values (keys) in PHP arrays are not restricted to integers. They may also be strings and other kinds of values. Similarly the value stored with a key can be any type of value ‐ an integer, string or another array.

key = "http" value = key = "ppyroxy" value = 'ppygroxysg.uwe.ac.uk:80' key = "header" value = 'Cache‐Control: no‐ cache'

Arrays are like Dictionaries in Java , hash or associative arrays in Perl.

Feb 2013 N. H. N. D. de Silva 12 Convert CSV string to an array of data

Now lets call the ffiunction to get the remote data file: The current value of $rawdatafile is passed to the function and becomes the value of the parameter $uri inside it.

$csv = uwe_get_file($rawdatafile);

Now split the string on the space character to get an array of strings which we can access as an array. $data = explode(" ", $csv);

Explode is a standard function in PHP ‐ note the weird order of parameters ‐ the separator character first!

Feb 2013 N. H. N. D. de Silva 13 Mapping the array indexes

We could just refer to the array indexes as defined on the WDL documentation ‐ $data[4] for temperature. A nicer way is to define some names for these numerical indexes

define ('WINDSPEED',1); define ('WINDDIRECTION',3); define('TEMPERATURE',4); define('STATION',32); define('TIMEHH',29); define ('TIMEMM',30); define('SUMMARY',49);

Now we can use the more readable form $data[TEMPERATURE] ‐ note there is no $ prefix with constants

Feb 2013 N. H. N. D. de Silva 14 Generate the RSS Now we can generate the RSS XML. Here we have lots of data to interpolate into the RSS template and use a 'heredoc' with delimiter. Expressions are enclosed in { } print << {$data[STATION]} {$site} Weather at {$da ta [TIMEHH]}: {$da ta [TIMEMM]} {$data[SUMMARY]} . Wind {$data[WINDSPEED"]} knots from {$data[WINDDIRECTION]} degrees. Temperature {$data[TEMPERATURE]} °C. Feb 2013EOT; N. H. N. D. de Silva 15 Mainline Finally the main program defines the overall processing using these functions. It is not a true pipeline because each step executes fully and puts its output in a temporary variable.

Run Source

Feb 2013 N. H. N. D. de Silva 16 Common code The function to get a file via the proxy and will be used in any applicati on whic h needs to access a from within UWE. Similarly the code to define the structure of the wdl data will be used in any script which access that data. We can put these in two separate files (why two?) and then include them in the source of any program which needs this code with the require() statement:

//main ...

Run Source

Feb 2013 N. H. N. D. de Silva 17 Concepts

o interaction between a client‐side form and server‐side ppgrocessing o variables and constants o functions transforming one or more inputs into an output o arrays with string keys as dictionaries or associative arrays o heredocs o setting a media‐type o factoring out common code

Feb 2013 N. H. N. D. de Silva 18