Using EMG Portal API Print
Friday, 17 October 2014

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.

The API is based on HTTP and the response is a JSON object, similar to '{"account":"test","balance":"1234.567"}'.

Authentication is done via HTTP Basic authentication supported by most http clients and languages, for example the PHP libcurl implementation used in the sample code below.

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;
}
?>