From 324d42ea3c2068eb4feaf8eba57cbb80b56700ef Mon Sep 17 00:00:00 2001 From: Vincent Casse Date: Wed, 13 Jan 2016 12:56:05 +0100 Subject: [PATCH 1/3] Add example to add http redirection Signed-off-by: Vincent Casse --- .../api_create_redirection.md | 125 ++++++++++++++++++ examples/create-Redirection/apiv6.php | 65 +++++++++ .../api_get_hosting_capacities.md | 8 +- 3 files changed, 194 insertions(+), 4 deletions(-) create mode 100644 examples/create-Redirection/api_create_redirection.md create mode 100644 examples/create-Redirection/apiv6.php diff --git a/examples/create-Redirection/api_create_redirection.md b/examples/create-Redirection/api_create_redirection.md new file mode 100644 index 0000000..6ce1066 --- /dev/null +++ b/examples/create-Redirection/api_create_redirection.md @@ -0,0 +1,125 @@ +How to get web hosting capabilities using php wrapper? +---------------------------------------------------- + +This documentation will help you to create an HTTP redirection from a subdomain to another domain. Following script include DNS record check and delete record if their will conflict with your redirection! + +## Requirements + +- Having PHP 5.2+ +- Having a dns record at OVH + +## Download PHP wrapper + +- Download the latest release **with dependencies** on github: https://github.com/ovh/php-ovh/releases + +```bash +# When this article was written, latest version is 2.0.0 +wget https://github.com/ovh/php-ovh/releases/download/v2.0.0/php-ovh-2.0.0-with-dependencies.tar.gz +``` + +- Extract it into a folder + +```bash +tar xzvf php-ovh-2.0.0-with-dependencies.tar.gz +``` + +- Create a new token +You can create a new token using these url: [https://api.ovh.com/createToken/?GET=/domain/zone/*&POST=/domain/zone/*&DELETE=/domain/zone/*](https://api.ovh.com/createToken/?GET=/domain/zone/*&POST=/domain/zone/*&DELETE=/domain/zone/*). Keep application key, application secret and consumer key to complete the script. + +Be warn, this token is only validated for this script and to use **/domain/zone/\*** APIs. +If you need a more generic token, you had to change right field. + +- Create php file to create your new HTTP redirection. You can download [this file](https://github.com/ovh/php-ovh/blob/master/examples/create-Redirection/apiv6.php) + +```php +get('/domain/zone/' . $domain . '/record'); + + foreach ($recordIds as $recordId) { + $record = $conn->get('/domain/zone/' . $domain . '/record/' . $recordId); + + // If record include A, AAAA or CNAME for subdomain asked, we delete it + if ( strcmp( $record['subDomain'], $subDomain) === 0 && + in_array( $record['fieldType'], array( 'A', 'AAAA', 'CNAME' ) ) ) { + + echo "We will delete field " . $record['fieldType'] . " for " . $record['zone'] . PHP_EOL; + $conn->delete('/domain/zone/' . $domain . '/record/' . $recordId); + } + } + + // We apply zone changes + $conn->post('/domain/zone/' . $domain . '/refresh'); + + // Now, we are ready to create our new redirection + $redirection = $conn->post('/domain/zone/' . $domain . '/redirection', array( + 'subDomain' => $subDomain, + 'target' => $targetDomain, + 'type' => $type, + 'title' => $title, + 'description' => $description, + 'keywords' => $keywords, + )); + + print_r( $redirection ); + +} catch ( Exception $ex ) { + print_r( $ex->getMessage() ); +} +?> +``` + +## Run php file + +```bash +php apiv6.php +``` + +For instance, with example values in script, the answer is +``` +( + [zone] => yourdomain.ovh + [description] => + [keywords] => + [target] => my_target.ovh + [id] => 1342424242 + [subDomain] => www + [type] => visible + [title] => +) +``` + +## What's more? + +You can discover all domain possibilities by using API console to show all available endpoints: [https://api.ovh.com/console](https://api.ovh.com/console) + diff --git a/examples/create-Redirection/apiv6.php b/examples/create-Redirection/apiv6.php new file mode 100644 index 0000000..c7c66eb --- /dev/null +++ b/examples/create-Redirection/apiv6.php @@ -0,0 +1,65 @@ +get('/domain/zone/' . $domain . '/record'); + + foreach ($recordIds as $recordId) { + $record = $conn->get('/domain/zone/' . $domain . '/record/' . $recordId); + + // If record include A, AAAA or CNAME for subdomain asked, we delete it + if ( strcmp( $record['subDomain'], $subDomain) === 0 && + in_array( $record['fieldType'], array( 'A', 'AAAA', 'CNAME' ) ) ) { + + echo "We will delete field " . $record['fieldType'] . " for " . $record['zone'] . PHP_EOL; + $conn->delete('/domain/zone/' . $domain . '/record/' . $recordId); + } + } + + // We apply zone changes + $conn->post('/domain/zone/' . $domain . '/refresh'); + + // Now, we are ready to create our new redirection + $redirection = $conn->post('/domain/zone/' . $domain . '/redirection', array( + 'subDomain' => $subDomain, + 'target' => $targetDomain, + 'type' => $type, + 'title' => $title, + 'description' => $description, + 'keywords' => $keywords, + )); + + print_r( $redirection ); + +} catch ( Exception $ex ) { + print_r( $ex->getMessage() ); +} +?> diff --git a/examples/hosting-getCapabilities/api_get_hosting_capacities.md b/examples/hosting-getCapabilities/api_get_hosting_capacities.md index 3026cc1..9142d16 100644 --- a/examples/hosting-getCapabilities/api_get_hosting_capacities.md +++ b/examples/hosting-getCapabilities/api_get_hosting_capacities.md @@ -13,14 +13,14 @@ This documentation will help you to get informations about your web hosting offe - Download the latest release **with dependencies** on github: https://github.com/ovh/php-ovh/releases ```bash -# When this article was written, latest version is 1.1.2 -wget https://github.com/ovh/php-ovh/releases/download/v1.1.2/php-ovh-1.1.2-with-dependencies.tar.gz +# When this article was written, latest version is 2.0.0 +wget https://github.com/ovh/php-ovh/releases/download/v2.0.0/php-ovh-2.0.0-with-dependencies.tar.gz ``` - Extract it into a folder ```bash -tar xzvf php-ovh-1.1.2-with-dependencies.tar.gz +tar xzvf php-ovh-2.0.0-with-dependencies.tar.gz ``` - Create a new token @@ -29,7 +29,7 @@ You can create a new token using these url: [https://api.ovh.com/createToken/?GE Be warn, this token is only validated for this script and for hosting called **my_domain**. Please replace **my_domain** by your web hosting reference! If you need a more generic token, you had to change right field. -- Create php file to get capabilities in the folder +- Create php file to get capabilities in the folder. You can download [this file](https://github.com/ovh/php-ovh/blob/master/examples/hosting-getCapabilities/apiv6.php) ```php Date: Wed, 13 Jan 2016 17:36:21 +0100 Subject: [PATCH 2/3] Fix typo Improve script Signed-off-by: Vincent Casse --- .../api_create_redirection.md | 83 ++----------------- examples/create-Redirection/apiv6.php | 15 ++-- .../api_get_hosting_capacities.md | 4 +- 3 files changed, 18 insertions(+), 84 deletions(-) diff --git a/examples/create-Redirection/api_create_redirection.md b/examples/create-Redirection/api_create_redirection.md index 6ce1066..ac15d17 100644 --- a/examples/create-Redirection/api_create_redirection.md +++ b/examples/create-Redirection/api_create_redirection.md @@ -13,7 +13,7 @@ This documentation will help you to create an HTTP redirection from a subdomain - Download the latest release **with dependencies** on github: https://github.com/ovh/php-ovh/releases ```bash -# When this article was written, latest version is 2.0.0 +# When this article is written, latest version is 2.0.0 wget https://github.com/ovh/php-ovh/releases/download/v2.0.0/php-ovh-2.0.0-with-dependencies.tar.gz ``` @@ -23,89 +23,24 @@ wget https://github.com/ovh/php-ovh/releases/download/v2.0.0/php-ovh-2.0.0-with- tar xzvf php-ovh-2.0.0-with-dependencies.tar.gz ``` -- Create a new token -You can create a new token using these url: [https://api.ovh.com/createToken/?GET=/domain/zone/*&POST=/domain/zone/*&DELETE=/domain/zone/*](https://api.ovh.com/createToken/?GET=/domain/zone/*&POST=/domain/zone/*&DELETE=/domain/zone/*). Keep application key, application secret and consumer key to complete the script. +## Create a new token -Be warn, this token is only validated for this script and to use **/domain/zone/\*** APIs. -If you need a more generic token, you had to change right field. +You can create a new token using this url: [https://api.ovh.com/createToken/?GET=/domain/zone/*&POST=/domain/zone/*&DELETE=/domain/zone/*](https://api.ovh.com/createToken/?GET=/domain/zone/*&POST=/domain/zone/*&DELETE=/domain/zone/*). Keep application key, application secret and consumer key to complete the script. -- Create php file to create your new HTTP redirection. You can download [this file](https://github.com/ovh/php-ovh/blob/master/examples/create-Redirection/apiv6.php) +Be warned, this token is only valid for this script on **/domain/zone/\*** APIs. +If you need a more generic token, you may adjust the **Rights** fields at your needs. -```php -get('/domain/zone/' . $domain . '/record'); - - foreach ($recordIds as $recordId) { - $record = $conn->get('/domain/zone/' . $domain . '/record/' . $recordId); - - // If record include A, AAAA or CNAME for subdomain asked, we delete it - if ( strcmp( $record['subDomain'], $subDomain) === 0 && - in_array( $record['fieldType'], array( 'A', 'AAAA', 'CNAME' ) ) ) { - - echo "We will delete field " . $record['fieldType'] . " for " . $record['zone'] . PHP_EOL; - $conn->delete('/domain/zone/' . $domain . '/record/' . $recordId); - } - } - - // We apply zone changes - $conn->post('/domain/zone/' . $domain . '/refresh'); - - // Now, we are ready to create our new redirection - $redirection = $conn->post('/domain/zone/' . $domain . '/redirection', array( - 'subDomain' => $subDomain, - 'target' => $targetDomain, - 'type' => $type, - 'title' => $title, - 'description' => $description, - 'keywords' => $keywords, - )); - - print_r( $redirection ); - -} catch ( Exception $ex ) { - print_r( $ex->getMessage() ); -} -?> -``` - -## Run php file +## Run script ```bash php apiv6.php ``` -For instance, with example values in script, the answer is +For instance, using the example values in this script, the answer would look like: ``` ( [zone] => yourdomain.ovh diff --git a/examples/create-Redirection/apiv6.php b/examples/create-Redirection/apiv6.php index c7c66eb..c30ef07 100644 --- a/examples/create-Redirection/apiv6.php +++ b/examples/create-Redirection/apiv6.php @@ -7,7 +7,7 @@ $applicationKey = "your_app_key"; $applicationSecret = "your_app_secret"; $consumer_key = "your_consumer_key"; -// Information about API and rights asked +// Information about API endpoint used $endpoint = 'ovh-eu'; // Information about your domain and redirection @@ -16,7 +16,7 @@ $subDomain = 'www'; // Here, the redirection will come from www.yourdomain.com $targetDomain = 'my_target.ovh'; $type = 'visible'; // can be "visible", "invisible", "visiblePermanent" -// Field to complete in case of invisible redirection +// Field to set in case of invisible redirection $title = ''; $keywords = ''; $description = ''; @@ -30,23 +30,19 @@ $conn = new Api( $applicationKey, try { // check if dns record are available - $recordIds = $conn->get('/domain/zone/' . $domain . '/record'); + $recordIds = $conn->get('/domain/zone/' . $domain . '/record?subDomain='. $subDomain ); foreach ($recordIds as $recordId) { $record = $conn->get('/domain/zone/' . $domain . '/record/' . $recordId); // If record include A, AAAA or CNAME for subdomain asked, we delete it - if ( strcmp( $record['subDomain'], $subDomain) === 0 && - in_array( $record['fieldType'], array( 'A', 'AAAA', 'CNAME' ) ) ) { + if ( in_array( $record['fieldType'], array( 'A', 'AAAA', 'CNAME' ) ) ) { echo "We will delete field " . $record['fieldType'] . " for " . $record['zone'] . PHP_EOL; $conn->delete('/domain/zone/' . $domain . '/record/' . $recordId); } } - // We apply zone changes - $conn->post('/domain/zone/' . $domain . '/refresh'); - // Now, we are ready to create our new redirection $redirection = $conn->post('/domain/zone/' . $domain . '/redirection', array( 'subDomain' => $subDomain, @@ -57,6 +53,9 @@ try { 'keywords' => $keywords, )); + // We apply zone changes + $conn->post('/domain/zone/' . $domain . '/refresh'); + print_r( $redirection ); } catch ( Exception $ex ) { diff --git a/examples/hosting-getCapabilities/api_get_hosting_capacities.md b/examples/hosting-getCapabilities/api_get_hosting_capacities.md index 9142d16..f7459f6 100644 --- a/examples/hosting-getCapabilities/api_get_hosting_capacities.md +++ b/examples/hosting-getCapabilities/api_get_hosting_capacities.md @@ -13,7 +13,7 @@ This documentation will help you to get informations about your web hosting offe - Download the latest release **with dependencies** on github: https://github.com/ovh/php-ovh/releases ```bash -# When this article was written, latest version is 2.0.0 +# When this article is written, latest version is 2.0.0 wget https://github.com/ovh/php-ovh/releases/download/v2.0.0/php-ovh-2.0.0-with-dependencies.tar.gz ``` @@ -24,7 +24,7 @@ tar xzvf php-ovh-2.0.0-with-dependencies.tar.gz ``` - Create a new token -You can create a new token using these url: [https://api.ovh.com/createToken/?GET=/hosting/web/my_domain&GET=/hosting/web/offerCapabilities](https://api.ovh.com/createToken/?GET=/hosting/web/my_domain&GET=/hosting/web/offerCapabilities). Keep application key, application secret and consumer key to complete the script. +You can create a new token using this url: [https://api.ovh.com/createToken/?GET=/hosting/web/my_domain&GET=/hosting/web/offerCapabilities](https://api.ovh.com/createToken/?GET=/hosting/web/my_domain&GET=/hosting/web/offerCapabilities). Keep application key, application secret and consumer key to complete the script. Be warn, this token is only validated for this script and for hosting called **my_domain**. Please replace **my_domain** by your web hosting reference! If you need a more generic token, you had to change right field. From 42c0a43c4b110982a80adfd55ac16904d438f180 Mon Sep 17 00:00:00 2001 From: Vincent Casse Date: Wed, 13 Jan 2016 17:41:33 +0100 Subject: [PATCH 3/3] Fix typo Signed-off-by: Vincent Casse --- .../api_create_redirection.md | 9 ++-- examples/create-Redirection/apiv6.php | 41 ++++++++++--------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/examples/create-Redirection/api_create_redirection.md b/examples/create-Redirection/api_create_redirection.md index ac15d17..1605d1d 100644 --- a/examples/create-Redirection/api_create_redirection.md +++ b/examples/create-Redirection/api_create_redirection.md @@ -1,12 +1,12 @@ -How to get web hosting capabilities using php wrapper? ----------------------------------------------------- +How to create HTTP redirection using php wrapper? +------------------------------------------------- This documentation will help you to create an HTTP redirection from a subdomain to another domain. Following script include DNS record check and delete record if their will conflict with your redirection! ## Requirements - Having PHP 5.2+ -- Having a dns record at OVH +- Having a DNS zone at OVH ## Download PHP wrapper @@ -25,7 +25,8 @@ tar xzvf php-ovh-2.0.0-with-dependencies.tar.gz ## Create a new token -You can create a new token using this url: [https://api.ovh.com/createToken/?GET=/domain/zone/*&POST=/domain/zone/*&DELETE=/domain/zone/*](https://api.ovh.com/createToken/?GET=/domain/zone/*&POST=/domain/zone/*&DELETE=/domain/zone/*). Keep application key, application secret and consumer key to complete the script. +You can create a new token using this url: [https://api.ovh.com/createToken/?GET=/domain/zone/*&POST=/domain/zone/*&DELETE=/domain/zone/*](https://api.ovh.com/createToken/?GET=/domain/zone/*&POST=/domain/zone/*&DELETE=/domain/zone/*). +Keep application key, application secret and consumer key to complete the script. Be warned, this token is only valid for this script on **/domain/zone/\*** APIs. If you need a more generic token, you may adjust the **Rights** fields at your needs. diff --git a/examples/create-Redirection/apiv6.php b/examples/create-Redirection/apiv6.php index c30ef07..1590aac 100644 --- a/examples/create-Redirection/apiv6.php +++ b/examples/create-Redirection/apiv6.php @@ -3,23 +3,23 @@ require __DIR__ . '/vendor/autoload.php'; use \Ovh\Api; // Informations about your application -$applicationKey = "your_app_key"; -$applicationSecret = "your_app_secret"; -$consumer_key = "your_consumer_key"; +$applicationKey = "your_app_key"; +$applicationSecret = "your_app_secret"; +$consumer_key = "your_consumer_key"; // Information about API endpoint used -$endpoint = 'ovh-eu'; +$endpoint = 'ovh-eu'; // Information about your domain and redirection -$domain = 'yourdomain.ovh'; -$subDomain = 'www'; // Here, the redirection will come from www.yourdomain.com -$targetDomain = 'my_target.ovh'; -$type = 'visible'; // can be "visible", "invisible", "visiblePermanent" +$domain = 'yourdomain.ovh'; +$subDomain = 'www'; // Here, the redirection will come from www.yourdomain.com +$targetDomain = 'my_target.ovh'; +$type = 'visible'; // can be "visible", "invisible", "visiblePermanent" // Field to set in case of invisible redirection -$title = ''; -$keywords = ''; -$description = ''; +$title = ''; +$keywords = ''; +$description = ''; // Get servers list $conn = new Api( $applicationKey, @@ -32,14 +32,17 @@ try { // check if dns record are available $recordIds = $conn->get('/domain/zone/' . $domain . '/record?subDomain='. $subDomain ); - foreach ($recordIds as $recordId) { - $record = $conn->get('/domain/zone/' . $domain . '/record/' . $recordId); - - // If record include A, AAAA or CNAME for subdomain asked, we delete it - if ( in_array( $record['fieldType'], array( 'A', 'AAAA', 'CNAME' ) ) ) { - - echo "We will delete field " . $record['fieldType'] . " for " . $record['zone'] . PHP_EOL; - $conn->delete('/domain/zone/' . $domain . '/record/' . $recordId); + // If subdomain is not defined, we don't want to delete all A, AAAA and CNAME records + if ( isset($subDomain) ) { + foreach ($recordIds as $recordId) { + $record = $conn->get('/domain/zone/' . $domain . '/record/' . $recordId); + + // If record include A, AAAA or CNAME for subdomain asked, we delete it + if ( in_array( $record['fieldType'], array( 'A', 'AAAA', 'CNAME' ) ) ) { + + echo "We will delete field " . $record['fieldType'] . " for " . $record['subDomain'] . $record['zone'] . PHP_EOL; + $conn->delete('/domain/zone/' . $domain . '/record/' . $recordId); + } } }