apiv6.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. require __DIR__ . '/vendor/autoload.php';
  3. use \Ovh\Api;
  4. // Informations about your application
  5. $applicationKey = "your_app_key";
  6. $applicationSecret = "your_app_secret";
  7. $consumer_key = "your_consumer_key";
  8. // Information about API endpoint used
  9. $endpoint = 'ovh-eu';
  10. // Information about your domain and redirection
  11. $domain = 'yourdomain.ovh';
  12. $subDomain = 'www'; // Here, the redirection will come from www.yourdomain.com
  13. $targetDomain = 'my_target.ovh';
  14. $type = 'visible'; // can be "visible", "invisible", "visiblePermanent"
  15. // Field to set in case of invisible redirection
  16. $title = '';
  17. $keywords = '';
  18. $description = '';
  19. // Get servers list
  20. $conn = new Api( $applicationKey,
  21. $applicationSecret,
  22. $endpoint,
  23. $consumer_key);
  24. try {
  25. // check if dns record are available
  26. $recordIds = $conn->get('/domain/zone/' . $domain . '/record?subDomain='. $subDomain );
  27. // If subdomain is not defined, we don't want to delete all A, AAAA and CNAME records
  28. if ( isset($subDomain) ) {
  29. foreach ($recordIds as $recordId) {
  30. $record = $conn->get('/domain/zone/' . $domain . '/record/' . $recordId);
  31. // If record include A, AAAA or CNAME for subdomain asked, we delete it
  32. if ( in_array( $record['fieldType'], array( 'A', 'AAAA', 'CNAME' ) ) ) {
  33. echo "We will delete field " . $record['fieldType'] . " for " . $record['subDomain'] . $record['zone'] . PHP_EOL;
  34. $conn->delete('/domain/zone/' . $domain . '/record/' . $recordId);
  35. }
  36. }
  37. }
  38. // Now, we are ready to create our new redirection
  39. $redirection = $conn->post('/domain/zone/' . $domain . '/redirection', array(
  40. 'subDomain' => $subDomain,
  41. 'target' => $targetDomain,
  42. 'type' => $type,
  43. 'title' => $title,
  44. 'description' => $description,
  45. 'keywords' => $keywords,
  46. ));
  47. // We apply zone changes
  48. $conn->post('/domain/zone/' . $domain . '/refresh');
  49. print_r( $redirection );
  50. } catch ( Exception $ex ) {
  51. print_r( $ex->getMessage() );
  52. }
  53. ?>