Created
January 3, 2020 22:45
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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