If you are unable to access the blesta_id from the session:
$user_id = (isset($_SESSION['blesta_id']) ? $_SESSION['blesta_id'] : null);
...then I would suggest following @Blesta Addons's recommendation of creating a plugin.
If you upgrade Blesta to v4.3.0+ then your plugin can make use of the new Requestor object that retrieves some relevant information on the currently-logged-in user. If the user is currently logged into Blesta, you should be able to retrieve their user information by creating a plugin with a model that fetches the user from the database, e.g.:
class MyPluginUserModel extends MyPluginModel
{
public function getCurrentUser()
{
// Load information on the current user
$requestor = $this->getFromContainer('requestor');
// Fetch additional information on the current user
if ($requestor->user_id !== null) {
Loader::loadModels($this, ['Users']);
// Fetch and return the user if one exists
if (($user = $this->Users->get($requestor->user_id))) {
return $user;
}
}
// Return null that no user was found
return null;
}
}
You just need to make an API call to your "MyPluginUserModel::getCurrentUser" to retrieve the user. If a non-null value is returned, the username would be available in the "username" property:
<?php
// Load the API
require_once "/home/username/public_html/blesta_api.php";
$user = "API USERNAME";
$key = "API KEY";
$url = "https://blestainstallationurl.com/api/";
$api = new BlestaApi($url, $user, $key);
// Fetch the current user's username if they are logged into Blesta
$username = null;
if (($user = $api->get("MyPlugin.MyPluginUserModel", "getCurrentUser"))) {
$username = $user->username;
}