<?php
/**
*
* More information available at api.fortunekookie.com
* 
*/

//Required is the FortuneKookie API Class
require "fortunekookie.api.class.php";

//This function is to retrieve the "front" and "back" data elements from the FortuneKookie XML file
function get_tag_contents($xmlcode,$tag) {
  $offset=0;
  $xmlcode = trim($xmlcode);

    #find the next start tag
    $start_tag=strpos ($xmlcode,"<".$tag.">",$offset);
    $offset = $start_tag;

    #find the closing tag for the start tag you just found
    $end_tag=strpos ($xmlcode,"</".$tag.">",$offset);

    #split off <$tag>... as a string, leaving the </$tag> 
    $fortune_tag = substr ($xmlcode,$start_tag,($end_tag-$start_tag));
    $start_tag_length = strlen("<".$tag.">");
    if (substr($fortune_tag,0,$start_tag_length)=="<".$tag.">"){
      #strip off stray start tags from the beginning
      $fortune_tag = substr ($fortune_tag,$start_tag_length);
    }
return $fortune_tag;
}

//This function is to retrieve the "lucky number" data elements from the FortuneKookie XML file
function get_lucky_nbr($xmlcode) {
	$offset = 0;
	$xmlcode = trim($xmlcode);
	$lucky_nbr = "";

    #find the start tag
    $start_tag=strpos($xmlcode,"<lucky_nbr ",$offset);
    $start_tag_length = strlen("<lucky_nbr ");

    #find the closing tag
    $end_tag=strpos($xmlcode,"></lucky_nbr>");

	$x = ($start_tag + $start_tag_length);
	$y = ($end_tag - $x);

    #split off both tags from lucky number stream
    $fortune_tag = substr($xmlcode,$x,$y);

	//Strip out the lucky numbers
	for ($i = 1; $i <= 6; $i++)
		{
		$nbr_pos_start = strpos($fortune_tag,"\"") + 1;
		$nbr_pos_end = strpos($fortune_tag,"\"",$nbr_pos_start);
		$nbr = substr($fortune_tag,$nbr_pos_start,($nbr_pos_end - $nbr_pos_start));
		if ($i <> 6)
			{
			$lucky_nbr .= $nbr." - ";
			$nbr_length = strlen($fortune_tag);
			$fortune_tag = substr($fortune_tag,($nbr_pos_end + 2));
			}
		else
			{
			$lucky_nbr .= $nbr;
			}
		}

return $lucky_nbr;
}

//The number of random fortunes requested 1 <= n <= 10
$nbr = 1;

//Request a 32-digit security code from us at http://api.fortunekookie.com
$code = "1234567890abcdef1234567890abcdef";

//Username is NULL (future functionality)
$username = NULL;

//Password is NULL (future functionality)
$password = NULL;

//Instantiate the FortuneKookie class
$fortune = new FortuneKookie($username,$password);

//Retrieve the fortunes in xml format
$xml = $fortune->getFortunes($nbr, $code);

//Display the full XML file - for debugging purposes
//echo '<pre>';
//echo htmlentities($xml);
//echo '</pre>';

//Sample of code to display the data components of one (1) fortune cookie fortune
$fortune_front = get_tag_contents($xml,"front");
$fortune_back = get_tag_contents($xml,"back");
$fortune_nbr = get_lucky_nbr($xml);
?>
<html>
	<head>
	<title>Random Fortune Cookie Fortune | Sample Code</title>
    <style type="text/css">
	  div.fortunekookie table {
	  	border-style: groove;
	  	padding: 15px;
		}
	  div.fortunekookie span {
		font-family: courier new,monospace;
		font-size: 14px;
		}
	  div.fortunekookie p {
		font-family: arial,sans-serif;
		font-size: 12px;
		text-align: right;
		}
	  div.fortunekookie a {
		text-decoration: none;
	  	color: blue;
		}
	  div.fortunekookie a:hover {
	  	text-decoration: underline;
	  	color: red;
	  	}
    </style>
	</head>
	<body>
	<div class="fortunekookie">
	<span>
	<table>
		<tr><td>Front:</td><td><?php echo $fortune_front?></td></tr>
		<tr><td>Back:</td><td><?php echo $fortune_back?></td></tr>
		<tr><td>Luck Numbers:</td><td><?php echo $fortune_nbr?></td></tr>
		<tr><td colspan=2>&nbsp;</td></tr>
		<tr><td colspan=2><p><a href="http://www.fortunekookie.com" target="_blank">Check out more fortunes at FortuneKookie.com</a></p></td></tr>
	</table>
	</span>
	</div>
	</body>
</html>