TestClient.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. class ApiClient {
  3. private string $apiUrl;
  4. private string $apiKey;
  5. /**
  6. * @param $apiUrl
  7. * @param $apiKey
  8. */
  9. public function __construct($apiUrl, $apiKey) {
  10. $this->apiUrl = rtrim($apiUrl, '/');
  11. $this->apiKey = $apiKey;
  12. }
  13. /**
  14. * Initially deploy the webserver environment for the customer
  15. *
  16. * @param $userName : The username under which the domain is deployed
  17. * @param $domain : The Domain to migrate
  18. * @param $adminName : The Super-Admin User of the CRM System (usually the e-mail of the customer
  19. * @param $adminPassword : A self randomly generated password
  20. *
  21. * @return array a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  22. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  23. *
  24. * Attention: The given parameters username, adminName and adminPassword must be
  25. * stored locally for the Single Sign on from whcms plugin
  26. */
  27. public function init($userName, $domain, $adminName, $adminPassword): array {
  28. $url = "$this->apiUrl/init/$userName/$domain";
  29. $data = [
  30. 'admin_name' => $adminName,
  31. 'admin_password' => $adminPassword
  32. ];
  33. return $this->sendRequest('POST', $url, $data);
  34. }
  35. /**
  36. * Disables the webpage
  37. *
  38. * @param username: The username under which the domain is deployed
  39. * @param domain: The Domain to migrate
  40. *
  41. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  42. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  43. */
  44. public function disable($domain, $userName): array {
  45. $url = "$this->apiUrl/disable/$userName/$domain";
  46. return $this->sendRequest('GET', $url);
  47. }
  48. /**
  49. * Enables the webpage
  50. *
  51. * @param username: The username under which the domain is deployed
  52. * @param domain: The Domain to migrate
  53. *
  54. * @return a json with ['status' => $httpCode,'response' => ['isenabled' => 'YES']];
  55. * or a json with ['status' => $httpCode,'response' => ['isenabled' => 'NO']];
  56. */
  57. public function enable($domain, $userName): array {
  58. $url = "$this->apiUrl/enableprod/$userName/$domain";
  59. return $this->sendRequest('GET', $url);
  60. }
  61. /**
  62. * Returns certifikate informations
  63. *
  64. * @param username: The username under which the domain is deployed
  65. * @param domain: The Domain to migrate
  66. *
  67. * @return a json with ['status' => $httpCode,'response' => ['ssl_expiry' => 'Datum des Ablaufs des Zertifikats', 'ssl_remaining' => 'Anzahl der Tage bis zum Ablauf des Zertifikats']];
  68. */
  69. public function getSSLDays($domain, $userName): array {
  70. $url = "$this->apiUrl/getssldays/$userName/$domain";
  71. return $this->sendRequest('GET', $url);
  72. }
  73. /**
  74. * Terminates a Website permanently
  75. *
  76. * @param $domain : The domain to terminate (terminates dev and prod website)
  77. * @param $userName : The username under which the domain is deployed
  78. * @return array : a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  79. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  80. */
  81. public function terminate($domain, $userName): array {
  82. $url = "$this->apiUrl/terminate/$userName/$domain";
  83. return $this->sendRequest('GET', $url);
  84. }
  85. // helpers
  86. private function sendRequest($method, $url, $data = []): array {
  87. $ch = curl_init();
  88. error_log("SEND REQUEST: " . print_r($data, true));
  89. $options = [
  90. CURLOPT_URL => $url,
  91. CURLOPT_RETURNTRANSFER => true,
  92. CURLOPT_HTTPHEADER => [
  93. "X-Api-Key: $this->apiKey",
  94. "Content-Type: application/json"
  95. ],
  96. CURLOPT_CONNECTTIMEOUT => 0,
  97. CURLOPT_TIMEOUT => 400,
  98. //CURLOPT_CUSTOMREQUEST => $method,
  99. CURLOPT_SSL_VERIFYPEER => false, // Deaktiviert die Überprüfung des SSL-Zertifikats
  100. CURLOPT_SSL_VERIFYHOST => false // Akzeptiert alle Hostnamen
  101. ];
  102. if ($method === 'POST' && !empty($data)) {
  103. $options[CURLOPT_POST] = true;
  104. // $options[CURLOPT_POSTFIELDS] = $data;
  105. $options[CURLOPT_POSTFIELDS] = json_encode($data,true);
  106. }
  107. curl_setopt_array($ch, $options);
  108. $response = curl_exec($ch);
  109. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  110. curl_close($ch);
  111. return [
  112. 'status' => $httpCode,
  113. 'response' => json_decode($response, true)
  114. ];
  115. }
  116. }
  117. // Beispielnutzung mit HTTPS
  118. $apiClient = new ApiClient('https://vm-dc-builderhost-01.thurdata.local', 'your-secure-password');
  119. //$response = $apiClient->getSSLDays('exampleuser', 'example.com');
  120. $response = $apiClient->deployDev('roka101', 'tech-design.ch', 'info@tech-design.ch', 'Technics2312');
  121. print_r($response);