1 / 18

UFCEKG-20-2 Data, Schemas & Applications

UFCEKG-20-2 Data, Schemas & Applications. Lecture 4 Server Side Scripting & PHP. Last week: . encode data for communication c ard based csv tagged records Xml xml vocabularies x ml processing vocabularies xml namespaces xml characteristics r ss rss aggregation.

ormand
Télécharger la présentation

UFCEKG-20-2 Data, Schemas & Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Last week: • encode data for communication • card based • csv • tagged records • Xml • xml vocabularies • xml processing vocabularies • xml namespaces • xml characteristics • rss • rss aggregation

  3. Server-side languages • Processing carried out on a web server • SSI - Server-Side Include with some processing • Perl • PHP • Python • ASP (C#, VB, F# ..) • JSP • Ruby • XSLT • XQuery • Javascript(with jNode)

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

  5. SSI (Server Side Includes) Processed by the web server File suffix - shtml.. Include a common file <!--#include virtual="../quote.txt" --> Execute a Unix command or cgi script and include the result <!--#exec cmd="ls -l"--> <!--#exec cgi="/cgi-bin/foo.cgi"-->

  6. PHP Origins Originally created by RasmusLerdorf (born Greenland, educated in Canada) around 1995 PHP originally abbreviated as ‘Personal Home Pages’, now known as ‘PHP Hypertext Pre-processor’ Other key developers: ZeevSurashi and AndiGutmans (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 ‘Hello World’ PHP is often used to generate HTML pages with dynamic content. Here it is used like SSI. The markup <?php and ?> are one way to delimit PHP code within HTML. <html> <head> <title>Basic Page </title> </head> <body> <h1>Basic Page</h1> <p>Hello world</p> <p>This page is different every time it is loaded </p> <p>Current date/time is <?php echo date("l, j M y H:i:s ") ?> </p> </body> </html> Run

  8. Accessing URI parameters <?php $site = $_REQUEST["site"]; $rawdatafile = $site . "/clientraw.txt"; echo $rawdatafile; ?> • PHP uses C conventions for assignment, statement separators, blocks etc. • PHP provides the parameters from the URI as an array of values. $_REQUEST • arrays can be indexed by position or key value • Variables start with the character $ • 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 <html> <head> <title>Form </title> </head> <body> <h1>Form </h1> <p>Current date <?php echo date("l, j M y H:i ") ?> </p> <form method="get" action=“request_param.php"> <label for="site">Enter the weather site URL</label> <input type="text" name="site" /> <input type="submit" value="Get Weather"/> </form> </body> </html> Run Notice that when the url is entered into the form, the browser url-encodes the special characters.

  10. 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]

  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 $rawdatafileis passed to the function and becomes the value of the parameter $uriinside 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 <?xml version="1.0"?> <rss version="2.0"> <channel> <title>{$data[STATION]}</title> <link>{$site}</link> <item> <title>Weather at {$data[TIMEHH]}:{$data[TIMEMM]}</title> <description> {$data[SUMMARY]} . Wind {$data[WINDSPEED"]} knots from {$data[WINDDIRECTION]} degrees. Temperature {$data[TEMPERATURE]} &#0176;C. </description> </item> </channel> </rss> 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. Concepts • interaction between a client-side form and server-side processing • variables and constants • functions transforming one or more inputs into an output • arrays with string keys as dictionaries or associative arrays • heredocs • setting a media-type • factoring out common code

More Related