Book-as-API example using "Comedy of Errors" by William Shakespeare

View Source HTML file View Source PHP file (this page)

Select Query Parameters:

Request Type:
(optional - for Specific Act Query)
Act:
Scene:
Return:

Results:"; $dom = new DomDocument(); $dom->preserveWhiteSpace = true; if (!$dom->loadHTMLFile('comedy.html')) { foreach (libxml_get_errors() as $error) { var_dump($error); } } $return = array(); if ($_POST['request_type'] == "Metadata") { $meta_tags = $dom->getElementsByTagName("meta"); echo "
";
		for ($i = 0; $i< $meta_tags->length; $i++) {
			$el = $meta_tags->item($i);
			if ($_POST['return_type']=="HTML") {
				echo "
".htmlentities($dom->saveHTML($el))."
"; } else { echo "Name: " . $el->getAttribute("name") . " Content: " . $el->getAttribute("content") . "
"; } } echo "
"; } else if ($_POST['request_type'] == "Dramatis Personae") { $dramatis = $dom->getElementById("dramatis_personae"); if ($_POST['return_type'] == "HTML") { echo "
".htmlentities($dom->saveHTML($dramatis))."
"; } else { echo "
".htmlentities(strip_tags($dom->saveHTML($dramatis)))."
"; } //$return->importNode($dramatis); } else if ($_POST['request_type'] == "Specific Act") { $potential_acts = $dom->getElementsByTagName("table"); for ($i = 0; $i<$potential_acts->length; $i++) { /* $act DOMElement() */ $act = $potential_acts->item($i); if (($act->getAttribute('data-scene') == $_POST['scene']) || ($_POST['scene'] == "Any")) { if (($act->getAttribute('data-act') == $_POST['act']) || ($_POST['scene'] == "Any")) { if ($_POST['return_type'] == "HTML") { echo "
".htmlentities($dom->saveHTML($act))."
"; } else { echo "
".htmlentities(strip_tags($dom->saveHTML($act)))."
"; } } } } } } ?>