Overview
From EMG Portal 1.12 it is possible to access an API to check account balance. To access the API a user must be created with the “Allow API calls” option checked and must have role “Admin”. The API is based on HTTP and the response is a JSON object, similar to the one below.
{
"account": "test",
"balance": "1234.567"
}
Authentication is done via HTTP Basic authentication, which is supported by most http clients and languages, including the PHP libcurl implementation used in the sample code below.
API calls
Account balance
Get account balance
Requires role “ADMIN”
GET /emgportal/api/accountbalance
{"account":"test","balance":"1234.567"}
Topup account
Requires role “OWNER”
Add specified amount to account balance. A negative value can be used to deduct from balance. A log entry will be added to the account balance history. The account name and balance after the topup will be returned in the response.
POST /emgportal/api/accountbalance
Parameters:
account - the account name
amount - the amount to add to account balance.
{"account":"test","balance":"1234.567"}
Sample PHP code
Below you find sample code for accessing API from PHP.
<?php
# Replace url, username and password below with correct information for your site
$url = 'https://www.example.com/emgportal/api/accountbalance';
$username = 'xxx';
$password = 'yyy';
$result = call_api($url, $username, $password);
if ($result !== FALSE) {
#print_r($result);
$obj = json_decode($result);
if(isset($obj->error)) {
print "API returned error, {$obj->error}\n";
} else {
print "account={$obj->account}\n";
print "balance={$obj->balance}\n";
}
}
function call_api($url, $username, $password)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($curl);
if($result === FALSE) {
print "API call failed, error=" . curl_error($curl) . "\n";
curl_close($curl);
return FALSE;
}
curl_close($curl);
return $result;
}?>