<?PHP
	#An attempt to use the test function which Rob Wells apparently left at the end of AuthentMessage.php.
	include("AuthentComms.php");
	
	function htmlize($str){
		$out = str_replace("<", "&lt;", $str);
		$out = str_replace(">", "&gt;", $out);
		$out = str_replace("\t", "   ", $out);
		$out = str_replace(" ", "&nbsp;", $out);
		$out = str_replace("\n", "<br>\n", $out);
		return $out;
	}
	
	function printData($data){
		ob_start();
		print_r($data);
		$output = ob_get_contents();
		ob_end_clean();
		$output = str_replace("\n", "<br>\n", $output);
		$output = str_replace(" ", "&nbsp;", $output);
		echo $output;
		
	}
	
	$reqxml ="<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<AuthentXML xmlns = \"http://xml.authentify.net/MessageSchema.xml\" version = \"1.0\">
<header>
<tsoid>Auth_Sandbox</tsoid>
<licensekey>dc391483-473c-4c3a-80d8-61ce0d0f60a9</licensekey>
<application>PhoneConfirm</application>
<asid>83442</asid>
</header>
<body>
<request>
<action>PhoneSession</action>
<data xmlns:dat='http://xml.authentify.net/CommonDataSchema.xml'>
<dat:confirmNumber>1234</dat:confirmNumber>
<dat:phoneNumber>617-413-0706</dat:phoneNumber>
<dat:name>
<dat:firstName>Rob</dat:firstName>
<dat:lastName>Wells</dat:lastName>
</dat:name>
<dat:address>
<dat:streetAddress>8745 West Higgins Road</dat:streetAddress>
<dat:postalCode>60631</dat:postalCode>
</dat:address>
</data>
</request>
</body>
</AuthentXML>
";
echo "We are dealing with raw XML:<br>".htmlize($reqxml,true);
$aParser = new AuthentXMLParser();
echo "<br>--- Request XML Test ---<br>\n";

$msg = $aParser->parse($reqxml);

if (is_null($msg))
{
	echo "<br>We gloriously failed to parse reqxml.";
}
else
{
	echo "<br>We parsed reqxml.<br>\nMessage was for ".$msg->tsoid." ".$msg->application.".<br>\nType of message was ".$msg->type.", status was ".$msg->statusCode."<br>\nMessage Data is :<br>\n";
	
    //Here we print the message using print_r
    printData($msg);

	echo "<br>Here is the same stuff returned to XML?";
	echo htmlize($msg->toXML());
	$comms = new AuthentComms("https://imp.authentify.com/s2s/default.asp");
	$newmsg = $comms->sendMessage($msg);
	if( !isset($newmsg) )
	{
		echo "<br>We have run into trouble sending the message.<br>";
		echo $comms->lastError;
	}
	else
	{
		echo "<br>We seem to have sent the message.<br>";
		echo htmlize($newmsg->toXML());
	}
	flush();
}
echo "<br><br>That's the end of the page, hope you enjoyed it.";
?>