Tags

, , ,


This little GEM took me ages to find so I thought it worthwhile to keep it handy, its a PHP program to talk to a Parallels Business Automation Server using SOAP. Ive modified it a little and cleaned up some of the code.

What it does and How it works

The program accesses the PBA server with a valid account (the email and password), obtains a session ID which is passed back in the headers and then calls a set of API calls to obtain data on set accounts and subscriptions. The calls are not important, its the transport class general layout with the correct parameters to enable access and wait for a response from the PBA server thats important.

The reason its significant is with the right/valid user credentials it actually works, unlike the demo code in the API manual which does not due to the lack of time out parameters in the SOAP calls and other headers.

<?php
$transport = new Transport;
$GLOBALS['StoreConf']['HSPCOMPLETE_SERVER'] = 'http://localhost';
define( 'CURRENT_API_VERSION', '1.0' );
$result = $transport->openSession('en');
print_r($result);
$account_id =  $result['account_id'];
echo "get_account_info\n";
$account_id=1754;
$result = $transport->call('get_account_info', array('account_id'=>$account_id), 'HSPC/API/Account');
echo "auth_person\n";
$result = $transport->call('auth_person', array('login_to_cp'=>1, 'email'=>'me@mail-server.localhost.org', 'password'=>'abc123'), 'HSPC/API/Person');
print_r($result);
echo "get_Account_subscr\n";
$result = $transport->call('get_account_subscr', array('account_id'=>$account_id), 'HSPC/API/Billing');
print_r($result);
$subscr_id=3751; /* for testing we will pull back subscription 3751 */
echo "get_subscr_info\n";
$result = $transport->call('get_subscr_info', array('subscr_id'=>$subscr_id,'account_id'=>$account_id), 'HSPC/API/Billing');
print_r($result);

class Transport
{
private $soapClient;

public function getSoapClient()
{
return $this->soapClient;
}

public function setSoapClient($client)
{
$this->soapClient = $client;
}

public function openSession($lang_id)
{
ini_set("default_socket_timeout", 600);
$this->soapClient = new SoapClient( NULL,
array(
"location" => $GLOBALS['StoreConf']['HSPCOMPLETE_SERVER'].'/hspc/xml-api',
"uri"      => 'HSPC/API/'.CURRENT_API_VERSION,
"style"    => SOAP_RPC,
"use"      => SOAP_ENCODED,
"connection_timeout" => 600)
);

$result = $this->call( 'session_open',
array(
'account_id'=>0,
'email'=>'me@localhost.org',
'password'=>'abc123',
'lang_id'=>$lang_id,
'account_id'=>0,
), 'HSPC/API/');

if($this->fault)
{
$result = $this->call( 'session_open',
array(
'email'=>isset($GLOBALS['StoreConf']['VENDOR_EMAIL']) ? $GLOBALS['StoreConf']['VENDOR_EMAIL'] : NULL,
'password'=>isset($GLOBALS['StoreConf']['VENDOR_PASSWORD']) ? $GLOBALS['StoreConf']['VENDOR_PASSWORD'] : NULL,
'lang_id'  => $lang_id),'HSPC/API/');
}
if(!$this->fault)
{
$this->soapClient->__setSoapHeaders(new SoapHeader('ns', 'HSPC-SID', $result["session_id"]));
}
return $result;
}

private function objectToArray($d)
{
if(is_object($d))
{
$d = get_object_vars($d);
}
if(is_array($d))
{
return array_map( array($this, __FUNCTION__), $d);
}
else
{
return $d;
}
}

public function call($function, $params, $namespace)
{
$time_start = microtime(true);
if(!preg_match('/\/\d+\.\d+$/', $namespace))
{
$namespace .= '/'.CURRENT_API_VERSION;
}
$result = '';
$this->fault = 0;
$this->faultcode ='';
$this->faultstring='';
try
{
$result = $this->soapClient->__soapCall( $function, $params ? array($params) : array(), array( "uri" => $namespace ));
$result = $this->objectToArray ($result);
}
catch (\Exception $e)
{
$this->fault = 1;
$this->faultcode = $e->faultcode;
$this->faultstring = $e->faultstring;
$this->detail = $this->objectToArray ( $e->detail );

$result["detail"] = $this->detail;
$result["faultcode"] = $this->faultcode;
$result["faultstring"] = $this->faultstring;
}
$time_end = microtime(true);
return $result;
}
} /* end of class Transport*/

?>

This code can be the basis of an extensive application exercising the PBA API.

Enjoy!

-oOo-

Advertisements