Location: PHPKode > scripts > Ip3 NetAccess XML API > ip3-netaccess-xml-api/ip3.php
<?php //BISMILLAH BISMILLAH BISMILLAH

/** 
 * 
 *	Example Class to handle Ip3 NetAccess Api
 *
 * @package		Ip3 NetAccess Api
 * @author		Ahsan Shahzad
 * @			NextBridge ltd.
 * @filesource
 */
	
// ------------------------------------------------------------------------

/**
 * Class for Ip3 NetAccess Api request and response handling
 *
 */

class Ip3
{
	var $host = null;
	var $shared_key=null;
	var $errors=array();
	var $fp=null;
	var $api_path=null;

	/*
	 * Class initialization
	 */
	function init($host,$shared_key)
	{
		$this->host=$host;
		$this->shared_key=$shared_key;
	}

	/*
	 * connect to the xml api
	 * Output: true on success, false on fail
	 */
	function connect()
	{
		/*======= Open a socket for HTTPS =======*/
		$this->fp = fsockopen("ssl://" . $this->host, 9098, $errno, $errstr, 30);


		/*========= Die on error initializing socket =========*/
		if ($errno == 0 && $this->fp == false)
		{
			$this->errors[]="Socket Error: Could not initialize socket.";
			return false;
		}
		elseif ($this->fp == false)
		{
			$this->errors[]="Socket Error #" . $errno . ": " . $errstr;
			return false;
		}
		
		return true;
	}

	/*
	 * Make Xml reqest to server and receive Xml response
	 * Input: string (Api path),string (Xml request data)
	 * Output: true on request success, false if problem in request making
	 */
	function ip3_make_xml_request($api_path,$request_data)
	{
		/* BISMILLAH */
		/* ======== Assemble the header to send =======*/

		$header  = '';
		$header .= "POST " . $api_path . " HTTP/1.0\r\n";
		$header .= "Host: " . $this->host . "\r\n";
		$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$header .= "Content-length: ".strlen($request_data)."\r\n\r\n";
		$header .= $request_data;

		/*======= Send the Header ========*/
		if(!@fputs($this->fp, $header))
		{
			$this->errors[]='Unable to send header.';
			return false;
		}
		return true;
	}

	/*
	 * Get the raw output from the server
	 * Output: string
	 */
	function getOutput()
	{
		$rawResult = "";
		
		while (!feof($this->fp))
			$rawResult .= fgets($this->fp, 128);

		/*======== Ignore headers =======*/
		$rawResultParts = explode("\r\n\r\n",$rawResult);
		$result = $rawResultParts[1];

		/*======== Output XML ========*/
		return $result;
	}

	/*
	 * Close the socket
	 */
	function disconnect()
	{
		fclose($this->fp);
	}
	
}	



// create a new instance of Ip3 class
$test= new Ip3;

// initialize values
$test->init('Ip.address.of.your.device','shared key');

// create connection with device 
$test->connect();

// Xml request data to send to device for showing all users registerd on device
$request_data='<?xml version ="1.0" ?>
				<netaccess shared_key="'.$test->shared_key.'">
					<category name="Basic_Configuration" action="Get">
						<module name="Control Panel Users" command="All_Users"/>
					</category>
				</netaccess>';

// sending Xml request
$test->ip3_make_xml_request("/admin/index.cgi",$request_data);

//get Xml response and process
$xmlstr=$test->getOutput();
if($xmlstr=='')
{
	print  "<br>No out Put received!";
}

//disconnect device connection
$test->disconnect();


/*=========== Reponse Display Logics ============*/
print "<p><b>------------- <font color=red>This script fetches all users registered at Ip3 based device (using NetAccess Api)</font> -------------</b><br>";
print "<p><b><font color=green>Here is Response Xml that we received:</font></b><br>";
print "<xmp>".$xmlstr."</xmp></p>";

print "<p><b><font color=green>Current Users on Device:</font></b><br>";
print $xmlstr."</p>";

print "<p><b><font color=green>Here is Data array of response Xml</font></b><pre>";

//get the output xml as an array using simple xml
$xml = new SimpleXMLElement($xmlstr);
print_r($xml);

print"</pre></p>";
//ALHAMDULILAH
?>
Return current item: Ip3 NetAccess XML API