Hier ein PHP Skript für die Liste der User, Domains, Datenbanken mittels Verwendung der API
- Generierung der Listen mittels jeweils einem statischen Funktionsaufruf
- Nur GET um die Listen zu bekommen
- Einfach anzupassendes Ergebnis z.B. mittels übersichtlichem Code und Debugausgabe in Console, oder Browser
- Caching von den Daten jeweils als Textfiles pro Endpoint (siehe Bild)
Code: Select all
<?php
//https://app.swaggerhub.com/apis-docs/keyhelp/api/2.1#/client
KeyHelpAPI::listKeyHelpObjects("clients"); print KeyHelpAPI::$newLine;
KeyHelpAPI::listKeyHelpObjects("domains"); print KeyHelpAPI::$newLine;
KeyHelpAPI::listKeyHelpObjects("databases"); print KeyHelpAPI::$newLine;
class KeyHelpAPI
{
public static $apiUrl="https://184.111.111.211/api/v2/";
public static $apiKey="kR8asdf.sBd3eK7F439ijfkmilkjmdioj09fujdsfjkdsf90u430rjhfoldjü09432u5rfjadlskfjklsdfjkasjdflksafd";
public static $apiMethod="GET";
public static $genFile="/home/users/pm/www/testdomain.de/cachedApiResults";
public static $newLine="\n";
public static function listKeyHelpObjects($objectEndpoint){
$arrObjects = json_decode(self::CallAPI($objectEndpoint,$data = false), true);
//use this to understand what data there is!
//print_r($arrObjects);
switch ($objectEndpoint)
{
case "clients":
//print $object[0]["username"];
self::initializeCachedAPICalls($objectEndpoint);
foreach ($arrObjects as $object) {
print $object["username"]. self::$newLine;
self::writeCachedAPICalls($objectEndpoint,$object["username"]);
}
break;
case "domains":
self::initializeCachedAPICalls($objectEndpoint);
foreach ($arrObjects as $object) {
print $object["domain"]. self::$newLine;
self::writeCachedAPICalls($objectEndpoint,$object["domain"]);
}
break;
case "databases":
self::initializeCachedAPICalls($objectEndpoint);
foreach ($arrObjects as $object) {
print $object["database_name"].":".$object["database_username"]. self::$newLine;
self::writeCachedAPICalls($objectEndpoint,$object["database_name"]);
}
break;
}
}
private static function initializeCachedAPICalls($objectEndpoint)
{
//Filename:
//print self::$genFile.'_'.$objectEndpoint.'.txt';
$h = fopen(self::$genFile.'_'.$objectEndpoint.'.txt', 'w');
fwrite($h, "");
}
private static function writeCachedAPICalls($objectEndpoint,$txtData)
{
$h = fopen(self::$genFile.'_'.$objectEndpoint.'.txt', 'a+');
fwrite($h, $txtData.PHP_EOL);
//fwrite($h, var_export($arrData, true)); //in case you want to write array directly
}
private static function CallAPI($objectEndpoint, $data = false)
{
$curl = curl_init();
$url = self::$apiUrl.$objectEndpoint;
$method = self::$apiMethod;
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, '0'); // 1 on Live-Systems
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, '0'); // 1 on Live-Systems
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'X-API-Key: '.self::$apiKey,
'accept: application/json',
'Content-Type: application/json',
));
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
}
?>
Nicht vergessen die IP vom Server freizugeben in der API Konfiguration im KeyHelp Admin.
Und Aufruf einfach außerhalb vom Webverzeichnis mit "php scriptName.php"
Weiteres Verarbeiten der generierten Files dann z.B. direkt in Bash um 'langsame' API-Aufrufe zu sparen:
Code: Select all
#!/bin/bash
cat cachedApiResults_clients.txt | while read line || [[ -n $line ]];
do
# do something with $line here
done