Presentation is loading. Please wait.

Presentation is loading. Please wait.

UFCEKG-20-2 Data, Schemas & Applications Lecture 4 Server Side Scripting & PHP.

Similar presentations


Presentation on theme: "UFCEKG-20-2 Data, Schemas & Applications Lecture 4 Server Side Scripting & PHP."— Presentation transcript:

1 UFCEKG-20-2 Data, Schemas & Applications Lecture 4 Server Side Scripting & PHP

2 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

3 Processing carried out on a web server 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) Server-side languages

4 We will be studying a number of different languages on this module because o Web development typically uses multiple languages o New languages are appearing all the time o Language design is evolutionary o Comparing the syntax and features of several languages helps to understand computer languages in general e.g. How do different languages handle StringsStrings Multiple languages

5 Processed by the web server File suffix - shtml.. Include a common file Execute a Unix command or cgi script and include the result SSI (Server Side Includes)

6 PHP Origins Originally created by Rasmus Lerdorf (born Greenland, educated in Canada) around 1995Rasmus Lerdorf 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 3.0 - a complete re-write of the original PHP/F1 engine (1997) PHP is Open Source software First version PHP/FI released 1995 PHP 5.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 ??

7 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 Page Basic Page Hello world This page is different every time it is loaded Current date/time is PHP ‘Hello World’ Run

8 Accessing URI parameters <?php $site = $_REQUEST["site"]; $rawdatafile = $site. "/clientraw.txt"; echo $rawdatafile; ?> 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

9 Form interface A common approach would be to provide an HTML form to allow the user to enter the parameters Form Form Current date Enter the weather site URL Notice that when the url is entered into the form, the browser url-encodes the special characters. Run

10 This will be constructed along the same lines as a Yahoo pipe, with functions in place of modules Yahoo pipe [Get data] >> [Parse CSV] >> [Compute new data] >> [create RSS] Weather pipe RSS feed Replacement

11 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. <?php function uwe_get_file($uri) { // get a file via UWE proxy and stop caching $context = stream_context_create( array('http'=> array('proxy'=>'proxysg.uwe.ac.uk:8080', 'header'=>'Cache-Control: no-cache' ) )); $contents = file_get_contents($uri,false,$context); return $contents; }; This defines a function which 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.

12 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 = "proxy" value = 'proxysg.uwe.ac.uk:80' key = "header" value = 'Cache-Control: no- cache' Arrays are like Dictionaries in Java, hash or associative arrays in Perl.

13 Convert CSV string to an array of data Now lets call the function 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!

14 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

15 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 <<<EOT {$data[STATION]} {$site} Weather at {$data[TIMEHH]}:{$data[TIMEMM]} {$data[SUMMARY]}. Wind {$data[WINDSPEED"]} knots from {$data[WINDDIRECTION]} degrees. Temperature {$data[TEMPERATURE]} °C. EOT;

16 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

17 Common code The function to get a file via the proxy and will be used in any application which needs to access a web resource 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: <?php require ("commonlib.php"); require("wdllib.php"); //main... Run Source

18 o interaction between a client-side form and server-side processing 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 Concepts


Download ppt "UFCEKG-20-2 Data, Schemas & Applications Lecture 4 Server Side Scripting & PHP."

Similar presentations


Ads by Google