#
# 375
# 410
# ...
# 518
#
if (!isset($_GET["name"]) || !isset($_GET["gender"])) {
header("HTTP/1.1 400 Invalid Request");
die("You didn't pass a name and gender!");
}
$name = $_GET["name"];
$gender = $_GET["gender"];
# rank.txt contains data about baby names, one per line
$lines = file("rank.txt", FILE_IGNORE_NEW_LINES);
foreach ($lines as $line) {
# $line = "Aaron m 147 193 187 199 250 237 230 178 52 34 34 41 55"
$tokens = explode(" ", $line);
if ($tokens[0] == $name && $tokens[1] == $gender) {
$dom = new DOMDocument();
$root = $dom->createElement("baby"); # ...
$root->setAttribute("name", $name);
$root->setAttribute("gender", $gender);
$dom->appendChild($root);
$year = 1890;
for ($i = 2; $i < count($tokens); $i++) {
$rank = $dom->createElement("rank"); # 375
$rank->appendChild($dom->createTextNode($tokens[$i]));
$rank->setAttribute("year", $year);
$root->appendChild($rank);
$year += 10;
}
header("Content-type: text/xml");
print($dom->saveXML());
break;
}
}
?>