mirror of
https://github.com/ovh/php-ovh.git
synced 2023-11-05 03:20:26 +01:00
Merge pull request #24 from VincentCasse/add_redirection_example
Add new example to add http redirection Signed-off-by: Jean-Tiare Le Bigot <jean-tiare.le-bigot@corp.ovh.com>
This commit is contained in:
commit
33c06797c5
61
examples/create-Redirection/api_create_redirection.md
Normal file
61
examples/create-Redirection/api_create_redirection.md
Normal file
@ -0,0 +1,61 @@
|
||||
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 zone at OVH
|
||||
|
||||
## Download PHP wrapper
|
||||
|
||||
- Download the latest release **with dependencies** on github: https://github.com/ovh/php-ovh/releases
|
||||
|
||||
```bash
|
||||
# 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
|
||||
```
|
||||
|
||||
- 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 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.
|
||||
|
||||
## Download the script
|
||||
|
||||
- Download and edit the php 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). **You had to replace some variables in the beginning of the file**.
|
||||
|
||||
## Run script
|
||||
|
||||
```bash
|
||||
php apiv6.php
|
||||
```
|
||||
|
||||
For instance, using the example values in this script, the answer would look like:
|
||||
```
|
||||
(
|
||||
[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)
|
||||
|
67
examples/create-Redirection/apiv6.php
Normal file
67
examples/create-Redirection/apiv6.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
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";
|
||||
|
||||
// Information about API endpoint used
|
||||
$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"
|
||||
|
||||
// Field to set in case of invisible redirection
|
||||
$title = '';
|
||||
$keywords = '';
|
||||
$description = '';
|
||||
|
||||
// Get servers list
|
||||
$conn = new Api( $applicationKey,
|
||||
$applicationSecret,
|
||||
$endpoint,
|
||||
$consumer_key);
|
||||
|
||||
try {
|
||||
|
||||
// check if dns record are available
|
||||
$recordIds = $conn->get('/domain/zone/' . $domain . '/record?subDomain='. $subDomain );
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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,
|
||||
));
|
||||
|
||||
// We apply zone changes
|
||||
$conn->post('/domain/zone/' . $domain . '/refresh');
|
||||
|
||||
print_r( $redirection );
|
||||
|
||||
} catch ( Exception $ex ) {
|
||||
print_r( $ex->getMessage() );
|
||||
}
|
||||
?>
|
@ -13,23 +13,23 @@ 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 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
|
||||
```
|
||||
|
||||
- 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
|
||||
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.
|
||||
|
||||
- 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
|
||||
<?php
|
||||
|
Loading…
Reference in New Issue
Block a user