PHP code example of chuckbartowski / ovh-sdk

1. Go to this page and download the library: Download chuckbartowski/ovh-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chuckbartowski / ovh-sdk example snippets


$ovh = new Ovh(new OvhClient($appKey, $appSecret, $consumerKey));

$ovh->domains()->addRecord('example.com', 'A', '203.0.113.10', 'www');
$ovh->cloud()->createInstance($projectId, ['name' => 'web01', 'flavorId' => '...', 'imageId' => '...']);
$ovh->dedicatedServers()->reboot('ns3000000.ip-1-2-3.eu');

$client = new OvhClient($appKey, $appSecret, null, 'ovh-eu');

$creds = $client->requestCredentials(
    accessRules: [
        ['method' => 'GET',    'path' => '/*'],
        ['method' => 'POST',   'path' => '/domain/*'],
        ['method' => 'DELETE', 'path' => '/domain/zone/*'],
    ],
    redirection: 'https://your-app.example.com/ovh/callback',
);

$consumerKey  = $creds->data('consumerKey');   // store this
$validationUrl = $creds->data('validationUrl'); // user opens this once to approve

use ChuckBartowski\OvhSdk\Client\OvhClient;
use ChuckBartowski\OvhSdk\Ovh;

$ovh = new Ovh(new OvhClient(
    applicationKey: getenv('OVH_APP_KEY'),
    applicationSecret: getenv('OVH_APP_SECRET'),
    consumerKey: getenv('OVH_CONSUMER_KEY'),
    endpoint: 'ovh-eu',
));

$account = $ovh->me()->info();
$ovh->domains()->addRecord('example.com', 'A', '203.0.113.10', 'www');
$ovh->domains()->refreshZone('example.com');

new OvhClient(
    string $applicationKey,
    string $applicationSecret,
    ?string $consumerKey = null,              // null → only requestCredentials() is allowed
    string $endpoint = 'ovh-eu',              // or a full custom base URL
    float $timeout = 30.0,
    bool $retryFailed = false,                // retry 429/5xx with exponential backoff
    int $maxRetries = 3,
    ?HttpClientInterface $httpClient = null,  // inject your own (proxy, scoped, mock…)
);

$id = $ovh->domains()->addRecord('example.com', 'MX', '10 mail.example.com.', '')->data('id');
$ovh->domains()->refreshZone('example.com');

$ovh->client()->get('/dedicated/nasha');
$ovh->client()->post('/cloud/project/'.$id.'/ai/notebook', $payload);

$response = $ovh->domains()->records('example.com');

$response->success;       // bool
$response->statusCode;    // int
$response->data;          // decoded JSON (OVHcloud often returns arrays of IDs)
$response->items();       // list form, e.g. [12, 34, 56]
$response->first();       // first item or null
$response->errorCode;     // OVHcloud error code on failure, e.g. 'NOT_FOUND'
$response->as(Model::class);      // hydrate a single object (bring your own model)
$response->asList(Model::class);  // hydrate a collection

try {
    $ovh->domains()->deleteRecord('example.com', $id);
} catch (ForbiddenException) {
    // consumer key was not granted DELETE on /domain/zone/*
}