Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, Victoria Kirst and Roy McElmurry IV. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
web service: software functionality that can be invoked through the internet using common protocols
header
header("Content-type: type/subtype");
header("Content-type: text/plain");
print "This output will appear as plain text now!\n";
header function to specify non-HTML output
| MIME type | related file extension |
|---|---|
| application/json | |
| text/plain | .txt |
| text/html | .html, .htm, ... |
| text/xml | .xml |
| text/css | .css |
| text/javascript | .js |
| image/gif | .gif |
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
<?php
header("Content-type: text/plain");
$base = (int) $_GET["base"];
$exp = (int) $_GET["exponent"];
$result = pow($base, $exp);
print $result;
?>
header to redirect between pages
header("Location: url");
if (!user_is_logged_in()) {
header("Location: user-login.php");
}
header to tell the browser to redirect itself to another page
name and gender and
finds and outputs the line from text file rank.txt with information about that name
Aaron m 147 193 187 199 250 237 230 178 52 34 34 41 55 Lisa f 0 0 0 0 0 733 220 6 2 16 64 295 720 ...For the following call:
http://example.com/babynames.php?name=Lisa&gender=fThe service should output the following line:
Lisa f 0 0 0 0 0 733 220 6 2 16 64 295 720
http://example.com/babynames.php?gender=f (no name passed!)
http://example.com/babynames.php?name=Borat&gender=m (not found in file)
print) are not ideal, because they could be confused for normal output
status property
| HTTP code | Meaning |
|---|---|
| 200 | OK |
| 301-303 | page has moved (permanently or temporarily) |
| 400 | illegal request |
| 401 | authentication required |
| 403 | you are forbidden to access this page |
| 404 | page not found |
| 410 | gone; missing data or resource |
| 500 | internal server error |
| complete list | |
header("HTTP/1.1 code description");
if ($_GET["foo"] != "bar") {
# I am not happy with the value of foo; this is an error
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");
function get_query_param($name) {
if (!isset($_GET[$name])) {
header("HTTP/1.1 400 Invalid Request");
die("HTTP/1.1 400 Invalid Request: missing required parameter '$name'");
}
if ($_GET[$name] == "") {
header("HTTP/1.1 400 Invalid Request");
die("HTTP/1.1 400 Invalid Request: parameter '$name' must be non-empty");
}
return $_GET[$name];
}
400 Invalid Request error if the correct parameters are not passed410 Gone error if the specified name and gender do not exist$_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" |
phpinfo(); to see a complete list
if ($_SERVER["REQUEST_METHOD"] == "GET") {
# process a GET request
...
} elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
# process a POST request
...
}
$_SERVER array's "REQUEST_METHOD" element
# suppose my web service accepts a "type" query parameter ...
<?php if ($_GET["type"] == "html") { ?>
<ul>
<?php foreach ($students as $kid) { ?>
<li> <?= $kid ?> </li>
<?php } ?>
</ul>
<?php } ?>
format parameter to the web serviceformat is html output HTML instead of the line of text.
The service should output HTML in the following format:
<h1>Aaron: m</h1> <ul> <li>1890: 147</li> <li>1900: 193</li> ... </ul>
...
header("Content-type: text/xml");
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
print "<books>\n";
foreach ($books as $book) {
print " <book title=\"{$book['title']}\" author=\"{$book['author']}\" />\n";
}
print "</books>\n";
text/xml or application/xml
<?xml line), then print XML data as output
printed
prints; write-only (hard to read existing XML data)
The PHP DOMDocument class represents an XML document. It has these methods:
createElement(tag)
|
create a new element node to add to the document |
createTextNode(text)
|
create a new text node to add to the document |
getElementById(id), getElementsByTagName(tag)
|
search for elements in the document |
load(filename), loadXML(string)
|
read XML data from a file on disk or from a string |
save(filename), saveXML()
|
write XML data to a file on disk or returns it as a string |
validate()
|
return whether the current document consists of valid XML data |
The PHP DOMElement class represents each DOM element. It has these fields/methods:
tagName, nodeValue
|
node's name (tag) and value (text) |
parentNode, childNodes, firstChild, lastChild, previousSibling, nextSibling
|
references to nearby nodes |
appendChild(DOMNode), insertBefore(newNode, oldNode), removeChild(DOMNode)
|
manipulate this node's list of children |
getElementsByTagName(tag)
|
search for descendent elements within this element |
getAttribute(name), setAttribute(name, value), removeAttribute(name)
|
get/set the value of an attribute on this tag |
... $xmldoc = new DOMDocument(); # <?xml version="1.0"?> $books_tag = $xmldoc->createElement("books"); $xmldoc->appendChild($books_tag); # <books> foreach ($books as $book) { $book_tag = $xmldoc->createElement("book"); # <book $book_tag->setAttribute("title", $book["title"]); # title="Harry Potter" /> $book_tag->setAttribute("author", $book["author"]); # author="J.K. Rowling" /> $books_tag->appendChild($book_tag); } # </books> header("Content-type: text/xml"); print $xmldoc->saveXML();
saveXML automatically inserts the XML prolog for usformat is xml output XML instead of the line of text.
The service should output XML in the following format:
<?xml version="1.0" encoding="UTF-8"?> <baby name="Morgan" gender="m"> <rank year="1890">375</rank> <rank year="1900">410</rank> ... <rank year="2010">518</rank> </baby>
...
header("Content-type: application/json");
print "{\n";
print " \"books\": [\n";
foreach ($books as $book) {
print " {\"title\": \"$title\"}\n";
}
print "\n";
text/json or application/json
prints; write-only (hard to read existing JSON data)
PHP includes the following global functions for interacting with JSON data:
json_decode(string)
|
parses the given JSON data string and returns an equivalent associative array object (like JSON.parse in JavaScript)
|
json_encode(object)
|
returns JSON equivalent for the given object or array or value (like JSON.stringify in JavaScript)
|
json_encode will output associative arrays as objects and normal arrays as arrays
<?php
$data = array(
"library" => "Odegaard",
"category" => "fantasy",
"year" => 2012,
"books" => array(
array("title" => "Harry Potter", "author" => "J.K. Rowling"),
array("title" => "The Hobbit", "author" => "J.R.R. Tolkien"),
array("title" => "Game of Thrones", "author" => "George R. R. Martin"),
array("title" => "Dragons of Krynn", "author" => "Margaret Weis"),
)
);
header("Content-type: application/json");
print json_encode($data);
?>
{
"library": "Odegaard",
"category": "fantasy",
"year": 2012,
"books": [
{"title": "Harry Potter", "author": "J.K. Rowling"},
{"title": "The Hobbit", "author": "J.R.R. Tolkien"},
{"title": "Game of Thrones", "author": "George R. R. Martin"},
{"title": "Dragons of Krynn", "author": "Margaret Weis"},
]
}
format is json output JSON instead of the line of text.
The service should output JSON in the following format:
{
name: "Morgan",
gender: "m",
rankings: [375, 410, 392, 478, 579, 507,
636, 499, 446, 291, 278, 332, 518]
}
We built a web service that outputs several types of data formats: