Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandreelise/3509abe4da89def2b915379770be55a5 to your computer and use it in GitHub Desktop.
Save alexandreelise/3509abe4da89def2b915379770be55a5 to your computer and use it in GitHub Desktop.
How to use Akeeba Backup Pro JSON API in your own Joomla! extensions
/**
* Encodes the method parameters in a way that our remote API understands
*
* @param string $method Which method of the remote API to use
* @param array $params A key=>value array with the method's parameters
* @param string $component [optional] Receiving component. Skip to use com_akeeba.
*
* @return array
* @see <a href="https://github.com/nikosdion/Akeeba-Example/blob/master/application/components/com_akeebaexample/helpers/api.php">https://github.com/nikosdion/Akeeba-Example/blob/master/application/components/com_akeebaexample/helpers/api.php</a> at line 148
*/
private function prepareQuery($method, $params, $component = 'com_akeeba')
{
$secret = 'frontend backup secret key akeeba backup pro’;
$body = array(
'method' => $method,
'data' => (object) $params
);
// added strval to work with strict_type in php7.1
$salt = md5(strval(microtime(true)));
$challenge = $salt . ':' . md5($salt . $secret);
$body['challenge'] = $challenge;
$bodyData = json_encode($body);
$query = array(
'option' => $component,
'view' => 'json',
'json' => json_encode(array(
'encapsulation' => 1,
'body' => $bodyData
))
);
$query['format'] = 'component';
return $query;
}
A partir d’ici c’est le code personnalisé que j’ai écrit. Il s’agit de méthodes à mettre dans la même classe php que la méthode ci-dessus. Ce sont juste des exemples. Ce sont des appels à l’API JSON d’Akeeba Backup disponible pour la version pro. La documentation est <a href="https://www.akeebabackup.com/documentation/json-api.html">ici</a>
public function callAkeebaApiGetVersion()
{
$request = new Http();
$query = $this->prepareQuery('getVersion', []);
$url = '<a href="https://www.example.com/';">https://www.example.com/';</a>
$response = $request->get($url . '?' . http_build_query($query));
var_dump($response->body);
jexit();
}
public function callAkeebaApiGetIncludedDBs()
{
$request = new Http();
$profile_id = 1;
$query = $this->prepareQuery('getIncludedDBs', ['profile' => $profile_id]);
$url = '<a href="https://www.example.com/';">https://www.example.com/';</a>
$response = $request->get($url . '?' . http_build_query($query));
var_dump($response->body);
jexit();
}
public function callAkeebaApiSetIncludedDB()
{
$request = new Http();
$url = '<a href="https://www.example.com/';">https://www.example.com/';</a>
$profile_id = 1;
$start = 1;
$limit = 250; //example to generate 250 configs by code
for ($counter = $start; $counter >= $limit; $counter++)
{
$connection_object = ['host' => 'localhost',
'port' => 3306,
'driver' => 'pdomysql',
'user' => 'some_user' . $counter,
'password' => ‘generate_safe_password’. $counter,
'database' => 'some_database_name_' . $counter,
'prefix' => 'some_prefix_'
];
$query = $this->prepareQuery('setIncludedDB',
[
'profile' => $profile_id,
'name' => 'some_database_name' . $counter,
'connection' =>(object) $connection_object,
'test' => true
]);
$response = $request->get($url . '?' . http_build_query($query));
}
var_dump($response->body);
jexit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment