deleteAttachedDomain.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. require __DIR__ . '/vendor/autoload.php';
  3. use \Ovh\Api;
  4. use GuzzleHttp\Client;
  5. // Informations about your application
  6. $applicationKey = "your_app_key";
  7. $applicationSecret = "your_app_secret";
  8. $consumer_key = "your_consumer_key";
  9. // Information about API and rights asked
  10. $endpoint = 'ovh-eu';
  11. // Informations about your web hosting and the attached domain (compulsory)
  12. $domain = 'mydomain.ovh'; // Web hosting id (often domain order with it)
  13. $domainToAttach = 'myotherdomaintoattach.ovh';
  14. $http_client = new Client([
  15. 'timeout' => 30,
  16. 'connect_timeout' => 5,
  17. ]);
  18. // Create a new attached domain
  19. $conn = new Api( $applicationKey,
  20. $applicationSecret,
  21. $endpoint,
  22. $consumer_key,
  23. $http_client);
  24. try {
  25. // This call will create a "task". The task is the status of the attached domain deletion.
  26. // You can follow the task on /hosting/web/{serviceName}/tasks/{id}
  27. $task = $conn->delete('/hosting/web/' . $domain . '/attachedDomain/' . $domainToDetach);
  28. echo "Task #" . $task['id'] . " is created" . PHP_EOL;
  29. // we check every 5 seconds if task is done
  30. // When the task disappears, the task is done
  31. while ( 1 ) {
  32. try {
  33. $wait = $conn->get('/hosting/web/' . $domain . '/tasks/' . $task['id']);
  34. if ( strcmp( $wait['status'], 'error' ) === 0 ) {
  35. // The task is in error state. Please check your parameters, retry or contact support.
  36. echo "An error has occured during the task" . PHP_EOL;
  37. break;
  38. } elseif ( strcmp( $wait['status'], 'cancelled' ) === 0 ) {
  39. // The task is in cancelled state. Please check your parameters, retry or contact support.
  40. echo "Task has been cancelled during the task" . PHP_EOL;
  41. break;
  42. }
  43. echo "Status of task #". $wait['id'] . " is '". $wait['status'] ."'" . PHP_EOL;
  44. } catch ( \GuzzleHttp\Exception\ClientException $ex) {
  45. $response = $ex->getResponse();
  46. if ( $response && $response->getStatusCode() === 404 ) {
  47. echo "Domain detached from the web hosting" . PHP_EOL;
  48. break;
  49. }
  50. throw $ex;
  51. }
  52. sleep(5);
  53. }
  54. } catch ( Exception $ex ) {
  55. print_r( $ex->getMessage() );
  56. }
  57. ?>