createAttachedDomain.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 ordered with it)
  13. $domainToAttach = 'myotherdomaintoattach.ovh';
  14. $path = 'otherFolder'; // Folder where website files are stored
  15. // Informations about the attached domain (optional)
  16. $cdn = 'none'; // Enable CDN on the attached domain. Can be NULL, 'none' or 'active'
  17. $firewall = 'none'; // Enable HTTP firewall (block dangerous pattern requests with Apache mod_security). Can be NULL, 'active' or 'none'
  18. $ownLog = 'myotherdomaintoattach.ovh'; // Separate logs by domain. Can be 'null' or domain offer you have at OVH
  19. $http_client = new Client([
  20. 'timeout' => 30,
  21. 'connect_timeout' => 5,
  22. ]);
  23. // Create a new attached domain
  24. $conn = new Api( $applicationKey,
  25. $applicationSecret,
  26. $endpoint,
  27. $consumer_key,
  28. $http_client);
  29. try {
  30. // This call will create a "task". The task is the status of the attached domain creation.
  31. // You can follow the task on /hosting/web/{serviceName}/tasks/{id}
  32. $task = $conn->post('/hosting/web/' . $domain . '/attachedDomain', array(
  33. 'domain' => $domainToAttach,
  34. 'path' => $path,
  35. 'cdn' => $cdn,
  36. 'firewall' => $firewall,
  37. 'ownLog' => $ownLog,
  38. ));
  39. echo "Task #" . $task['id'] . " is created" . PHP_EOL;
  40. // we check every 5 seconds if the task is done
  41. // When the task disappears, the task is done
  42. while ( 1 ) {
  43. try {
  44. $wait = $conn->get('/hosting/web/' . $domain . '/tasks/' . $task['id']);
  45. if ( strcmp( $wait['status'], 'error' ) === 0 ) {
  46. // The task is in error state. Please check your parameters, retry or contact support.
  47. echo "An error has occured during the task" . PHP_EOL;
  48. break;
  49. } elseif ( strcmp( $wait['status'], 'cancelled' ) === 0 ) {
  50. // The task is in cancelled state. Please check your parameters, retry or contact support.
  51. echo "Task has been cancelled during the task" . PHP_EOL;
  52. break;
  53. }
  54. echo "Status of task #". $wait['id'] . " is '". $wait['status'] ."'" . PHP_EOL;
  55. } catch ( \GuzzleHttp\Exception\ClientException $ex) {
  56. $response = $ex->getResponse();
  57. if ( $response && $response->getStatusCode() === 404 ) {
  58. echo "Domain attached to the web hosting" . PHP_EOL;
  59. break;
  60. }
  61. throw $ex;
  62. }
  63. sleep(5);
  64. }
  65. } catch ( Exception $ex ) {
  66. print_r( $ex->getMessage() );
  67. }
  68. ?>