Lecture 21 
 Web 2.0 and Web Services
				
					Except where otherwise noted, the contents of this presentation are Copyright 2010 Marty Stepp and Jessica Miller.
				
				
			 
			
			
			
			
				What is "Web 2.0"?
				
				
				
				
					- 
						Web 2.0: A set of ideas and technologies for creating modern, interactive web applications
						
						
							- 
								Ajax, multimedia, streaming, stateful pages, cookies, user-generated content, web services, ...
							
 
						
					 
				
			 
			
				What is a web service?
				
				
					web service: software functionality that can be invoked through the internet using common protocols
				
				
				
					- like a remote function(s) you can call by contacting a program on a web server
 
					- many web services accept parameters and produce results
 
					- can be written in PHP and contacted by the browser in XHTML and/or Ajax code
 
					- service's output is often not HTML but rather text, XML, or other content types
 
				
			 
			
				
					Content ("MIME") types
					(1.2.3)
				
				
					
						| MIME type | 
						related file extension | 
					
					
						| text/plain | .txt | 
					
					
						| text/html | .html, .htm, ... | 
					
					
						| text/css | .css | 
					
					
						| text/javascript | .js | 
					
					
						| text/xml | .xml | 
					
					
						| image/gif | .gif | 
					
					
						| image/jpeg | .jpg, .jpeg | 
					
					
						| video/quicktime | .mov | 
					
					
						| application/octet-stream | .exe | 
					
				
				
			 
			
				Setting content type with header
				
header("Content-type: type/subtype");
				
header("Content-type: text/plain");
print("This output will appear as plain text now!\n");
				
					- by default, a PHP script's output is assumed to be HTML
 
					- use the 
header function to specify non-HTML output
						
							- must appear before any other output generated by the script
 
						
					 
				
			 
			
			
			
			
				Example: Exponent web service
				
					- 
						Write a web service that accepts a 
base and exponent and outputs base raised to the exponent power.  For example, the following query should output 81 :
						
http://example.com/exponent.php?base=3&exponent=4
					 
					- 
						solution:
						
header("Content-type: text/plain");
$base = $_REQUEST["base"];
$exp = $_REQUEST["exponent"];
$result = pow($base, $exp);
print $result;
			 
 
			
			
			
			
				The $_SERVER superglobal array
				
				
					
						| index | 
						description | 
						example | 
					
					
					
						$_SERVER["SERVER_NAME"] | 
						name of this web server | 
						"webster.cs.washington.edu" | 
					
					
					
						$_SERVER["SERVER_ADDR"] | 
						IP address of web server | 
						"128.208.179.154" | 
					
					
					
						$_SERVER["REMOTE_HOST"] | 
						user's domain name | 
						"hsd1.wa.comcast.net" | 
					
					
					
						$_SERVER["REMOTE_ADDR"] | 
						user's IP address | 
						"57.170.55.93" | 
					
					
					
						$_SERVER["HTTP_USER_AGENT"] | 
						user's web browser | 
						"Mozilla/5.0 (Windows; ..." | 
					
					
					
						$_SERVER["HTTP_REFERER"] | 
						where user was before this page | 
						"http://www.google.com/" | 
					
					
					
						$_SERVER["REQUEST_METHOD"] | 
						HTTP method used to contact server | 
						"GET" or "POST" | 
					
				
				
				
			 
			
			
			
			
				Emitting partial-page HTML data
				
				
<?php if ($_REQUEST["type"] == "html") { ?>
	<ul>
		<?php foreach ($students as $kid) { ?>
			<li> <?= $kid ?> </li>
		<?php } ?>
	</ul>
<?php } ?>
				
					- 
						some web services do output HTML, but not a complete page
					
 
					- 
						the partial-page HTML is meant to be fetched by Ajax and injected into an existing page
					
 
				
			 
			
			
	
			
				Emitting XML data
				
				
...
header("Content-type: text/xml");
print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
?><books>
<?php foreach ($books as $title) { ?>
	<book title="<?= $title ?>" />
<?php } ?>
</books>
				
					- 
						specify a content type of 
text/xml or application/xml
					 
					- 
						print an XML prologue (the 
<?xml line) first
						
							- 
								important: no whitespace output can precede the prologue; must be 
printed
							 
						
					 
					- 
						then print each line of XML data/tags as output
					
 
					- 
						some PHP libraries automatically generate XML for you from other data (e.g. databases)
					
 
				
			 
			
			
			
			
				Reporting errors
				
					- 
						how does a web service indicate an error to the client?
						
							- 
								error messages (
print) are not ideal, because they could be confused for normal output
							 
						
					 
					- web service should return an HTTP "error code" to the browser, possibly followed by output
						
							- 
								these are the codes you see in Firebug's console and in your Ajax request's 
.status property
							 
						
						
							
								| HTTP code | Meaning | 
							
							
								| 200 | 
								OK | 
							
							
								| 301-303 | 
								page has moved (permanently or temporarily) | 
							
							
								| 400 | 
								illegal request | 
							
							
								| 403 | 
								you are forbidden to access this page | 
							
							
								| 404 | 
								page not found | 
							
							
								| 500 | 
								internal server error | 
							
							
								| complete list | 
							
						
					 
				
			 
			
				Using headers for HTTP error codes
				
header("HTTP/1.1  code  description");
				
if ($_REQUEST["foo"] != "bar") {
	
	header("HTTP/1.1 400 Invalid Request");
	die("An HTTP error 400 (invalid request) occurred.");
}
				
if (!file_exists($input_file_path)) {
	header("HTTP/1.1 404 File Not Found");
	die("HTTP error 404 occurred: File not found ($input_file_path)");
}
				
					- 
						
header can also be used to send back HTTP error codes
						
						
							header("HTTP/1.1 403 Forbidden"); 
							header("HTTP/1.1 404 File Not Found"); 
							header("HTTP/1.1 500 Server Error"); 
						
					 
				
			 
			
			
			
			
				Checking for mandatory query parameters
				
function require_params($params) {
	
	$params = func_get_args();
	foreach ($params as $param) {
		if (!isset($_REQUEST[$param])) {
			header("HTTP/1.1 400 Invalid Request");
			die("HTTP/1.1 400 Invalid Request: missing required parameter $param");
		}
	}
}
				
					- 
						
func_get_args function allows a function to accept a varying # of params