API Documentation RunningExamples server.get possible failover servers PHP

Aus EUserv Wiki

Wechseln zu: Navigation, Suche

server.get_possible_failover_servers

 
<?php
include_once('lib/xmlrpc.inc');
$xmlrpc_internalencoding = 'UTF-8';

//config
$host = 'api.test.euserv.net';
$port = 443;
$username = '<API_USER>';
$password = '<API_USER_PASSWORD>';
$api_path = '';

//init result  var
$result = array();

//set command and parameter
$command = 'server.get_possible_failover_servers';
$parameter = array();


class euserv_api 
{
	var $host = '';
	var $port = '';
	var $user = '';
	var $pass = '';
	var $api_path = '';
	var $result_xml = '';
	var $result_obj;
	var $clienturl = '';
	
	function login($host='',$port='',$username='',$password='',$api_path='')
	{
		$result = 0;
		$this->fault = 0;
		$this->host = $host;
		$this->port = $port;
		$this->user = $username;
		$this->pass = $password;
		$this->api_path = $api_path;
		$this->clienturl = 'https://'.$this->host.':'.$this->port.'/'.$api_path;
		$f = new xmlrpcmsg('debug.get_api_version');
		$f->addParam(new xmlrpcval(
		array
		(
			'login' => new xmlrpcval($this->user, 'string'),
			'password' => new xmlrpcval($this->pass, 'string')
		)
		,'struct'));

		$c = new xmlrpc_client($this->clienturl);
		$c->setSSLVerifyPeer(0);
		$r = $c->send($f,5,'https');
		if (!$r->faultCode()) 
		{
			$result_assoc = $this->load_assoc($r->value());
		}
		else
		{
			$result_assoc = array();
			$result_assoc['faultCode'] = $r->faultCode();
			$result_assoc['faultString'] = $r->faultString();
		}
		unset($c);
		unset($r);
		return $result_assoc;		
	}

	function fetch_assoc($command='',$parameter=array())
	{
		$this->result_xml = '';
		$keya = array_keys($parameter);
		$ic = 0;
		$paramar = array();
		$paramar['login'] = new xmlrpcval($this->user, 'string');
		$paramar['password'] = new xmlrpcval($this->pass, 'string');
		while ($ic < count($parameter))
		{			
			if (is_array($parameter[$keya[$ic]]))
			{
				$paramar[$keya[$ic]] = $this->convert_params($parameter[$keya[$ic]]);
			}
			else
			{
				$paramar[$keya[$ic]] = new xmlrpcval($parameter[$keya[$ic]]);
			}
			$ic = $ic+1;
		}		
		$f = new xmlrpcmsg($command);
		$f->addParam(new xmlrpcval($paramar,'struct'));				
		$c = new xmlrpc_client($this->clienturl);
		$c->setSSLVerifyPeer(0);	
		$r = $c->send($f,10,'https');
		if (!$r->faultCode()) 
		{			
			$result_assoc = $this->load_assoc($r->value());
		}
		else
		{
			$result_assoc = array();
			$result_assoc['faultCode'] = $r->faultCode();
			$result_assoc['faultString'] = $r->faultString();
		}
		unset($c);
		unset($r);
		return $result_assoc;		
	}
	function load_assoc($xmlrpcobj)
	{
		$result_arr = array();

		if(!($xmlrpcobj->kindOf() == 'struct'))
		{
			return $result_arr;
		}		
	    while (list($keyname, $data) = $xmlrpcobj->structEach())
	    {
	      $tmp_obj = array();
	      $tmp_obj = $this->load_assoc($data);
	      
	      if(!count($tmp_obj))
	      {
	      	$result_arr[$keyname.''] =  $data->scalarval();
	      }
	      else 
	      {
	      	$result_arr[$keyname.''] = $tmp_obj;
	      }	      
	    }
	    return $result_arr;
	}

	function convert_params($parameter)
	{
			$param = $parameter;
			$keya = array_keys($parameter);
			$paramar = array();
			$ic = 0;
			while ($ic < count($param))
			{
				if (is_array($param[$keya[$ic]]))
				{
					$paramar[$keya[$ic]] = $this->convert_params($param[$keya[$ic]]);
				}
				else
				{
					$paramar[$keya[$ic]] = new xmlrpcval($param[$keya[$ic]]);
				}
				$ic = $ic+1;
			}
			return new xmlrpcval($paramar,'struct');
	}
}


//create object from class
$euserv_api = new euserv_api();

//login and get version
$euserv_api->login($host, $port, $username, $password,$api_path);

//execute command and get result as associative array
$result = $euserv_api->fetch_assoc($command,$parameter);

//print result with print_r
echo '<html><pre>';
print_r($result);
echo '</pre></html>';
?>