Outdated Resource Warning!

This is documentation for legacy version 0.x releases. If you are using 1.x see the wiki.
<< API Output ExampleContents 

API Code Examples

Simple PHP Script to Dump XML Output to Screen

<?php
$target = "http://FREENATS_URL/api.php?mode=xml";


$query=array();
$param=array();

$query[0]="alerts";

$query[1]="node";
$param[1]="bob";

$key=""; // Set Your API Key Here

$url=$target."&apikey=".$key;

foreach($query as $key => $val)
	{
	$url.="&query[".$key."]=".$val;
	if (isset($param[$key])) $url.="&param[".$key."]=".$param[$key];
	}
	
echo "<html><title>FreeNATS API XML</title>";
echo "<b>".$url."</b><br><br>";


echo "<pre>";

$depth = 0;

function startElement($parser, $name, $attrs) 
{
    global $depth;
    for ($i = 0; $i < $depth; $i++) {
        echo "  ";
    }
    echo "+ $name ";
    foreach($attrs as $key => $val)
    	{
	    echo $key."=".$val." ";
    	}
    echo "\n";
    $depth++;
}

function endElement($parser, $name) 
{
    global $depth;
    $depth--;
}

function characterData($parser, $data) 
{
	$data=trim($data); // remove whitespace
	if ($data!="")
		{
	    global $depth;
	    for ($i = 0; $i < $depth; $i++) {
	        echo "  ";
	    }
	    
	   	echo $data."\n";
		}
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($url, "r"))) {
    die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($xml_parser)),
                    xml_get_current_line_number($xml_parser)));
    }
}
fclose($fp);
xml_parser_free($xml_parser);
echo "</pre>";

?>


Simple JavaScript/HTML Example

<html><title>FreeNATS API JavaScript</title>
<body>
<script type="text/javascript">



function HandleData(data)
{
document.write("<pre>");
if (data[0].length>0) // data[0] is the query[0] result array for alerts
	{
	document.write("There are alerts\n");
	}
else document.write("There are no alerts\n");

document.write("\nStatus for Node Queried in Q1\n");
for (i=0; i<data[1].length; i++)
	{
	document.write(data[1][i][0]+"="+data[1][i][1]+"\n");
	}

document.write("</pre>");
}



// Now Build the URL
var url="http://FREENATS_URL/api.php?mode=js";
url=url+"&apikey=";

url=url+"&query[0]=alerts";

url=url+"&query[1]=node";
url=url+"&param[1]=bob";

url=url+"&callback=HandleData";

document.write(url+"\n");

var output="<script src=\"";
output=output+url;
output=output+"\" ";
output=output+"type=\"";
output=output+"text/javascript\"";
output=output+">";
document.write(output);



</script>





<< API Output ExampleContents