PHP code example of dsolodev / namecheap-sdk

1. Go to this page and download the library: Download dsolodev/namecheap-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/ */

    

dsolodev / namecheap-sdk example snippets




declare(strict_types=1);

use Namecheap\ApiClient;
use Namecheap\Services\DomainService;
use Namecheap\Services\SslService;
use Namecheap\Services\UserService;
use Namecheap\Response\NamecheapResponse;

// Create API client
$apiClient = new ApiClient(
    apiUser: 'your_api_user',
    apiKey: 'your_api_key', 
    userName: 'your_username',
    clientIp: '192.168.1.100'
);

// Enable sandbox for testing (optional)
$apiClient->enableSandbox();

// Domain management with structured response
$domainService = new DomainService($apiClient);
$response = $domainService->getList();

if ($response->isSuccess()) {
    $domains = $response->getData();
    echo "Found " . count($domains) . " domains\n";
} else {
    foreach ($response->getErrors() as $error) {
        echo "Error: $error\n";
    }
}

// SSL certificate management
$sslService = new SslService($apiClient);
$sslResponse = $sslService->getList();

// Access data easily
$certificates = $sslResponse->getData();
$executionTime = $sslResponse->getExecutionTime();

use Namecheap\ApiClient;

$apiClient = new ApiClient(
    apiUser: 'your_api_user',
    apiKey: 'your_api_key',
    userName: 'your_username', 
    clientIp: '192.168.1.100',
    curlOptions: [
        CURLOPT_TIMEOUT => 30,
        CURLOPT_SSL_VERIFYPEER => true,
        CURLOPT_USERAGENT => 'MyApp/1.0'
    ]
);

$response = $domainService->getList();

// Check success status
if ($response->isSuccess()) {
    // Access data
    $data = $response->getData();
    
    // Get execution time  
    $time = $response->getExecutionTime();
    
    // Access metadata
    $command = $response->getCommand();
} else {
    // Handle errors
    $errors = $response->getErrors();
}

// Convert to different formats if needed
$array = $response->toArray();  // Complete response as array
$json = $response->toJson();    // JSON string
$xml = $response->toXml();      // Original XML

use Namecheap\Response\NamecheapResponse;

// All service methods return NamecheapResponse
$response = $domainService->create($domainInfo, $contactInfo);

if ($response->isSuccess()) {
    // Handle successful response
    $data = $response->getData();
    echo "Domain registered successfully!";
} else {
    // Handle errors - no try/catch needed!
    foreach ($response->getErrors() as $error) {
        echo "Error: $error\n";
    }
}

// Check for warnings
if ($response->hasWarnings()) {
    foreach ($response->getWarnings() as $warning) {
        echo "Warning: $warning\n";
    }
}

// Authentication errors
$response = $domainService->getList(); // Missing API credentials
if (!$response->isSuccess()) {
    echo $response->getFirstError(); // "[1010101] Authentication information must be provided."
}

// Validation errors  
$response = $domainService->create([], []); // Missing {
    echo $response->getFirstError(); // "[2019166] Domain is not available"
}

$domainService = new DomainService($apiClient);

// Get domain list with structured response
$response = $domainService->getList(
    searchTerm: 'example',
    listType: 'ALL',
    page: 1,
    pageSize: 20
);

if ($response->isSuccess()) {
    $domains = $response->getData();
    echo "Execution time: " . $response->getExecutionTime() . "ms\n";
}

// Check domain availability
$availability = $domainService->check(['example.com', 'example.net']);
if ($availability->isSuccess()) {
    $results = $availability->getData();
}

// Register domain
$registration = $domainService->create($domainInfo, $contactInfo);
if ($registration->isSuccess()) {
    echo "Domain registered successfully!\n";
} else {
    foreach ($registration->getErrors() as $error) {
        echo "Registration error: $error\n";
    }
}

use Namecheap\Services\DomainDnsService;

$dnsService = new DomainDnsService($apiClient);

// Get DNS hosts
$hosts = $dnsService->getHosts('example.com');

// Set DNS hosts
$result = $dnsService->setHosts('example.com', $hostRecords);

// Get email forwarding
$forwarding = $dnsService->getEmailForwarding('example.com');

use Namecheap\Services\SslService;

$sslService = new SslService($apiClient);

// Get SSL certificates
$certificates = $sslService->getList();

// Create SSL certificate
$result = $sslService->create($certificateInfo);

// Activate SSL certificate
$result = $sslService->activate($certificateId, $activationInfo);

use Namecheap\Services\UserService;

$userService = new UserService($apiClient);

// Get account balances
$balances = $userService->getBalances();

// Get pricing information
$pricing = $userService->getPricing();

// Update user information
$result = $userService->update($userInfo);

// Old way (v0.x)
$api = new Namecheap\Api($credentials);
$domains = $api->domains()->getList();

// New way (v1.0+)
$apiClient = new ApiClient($credentials);
$domainService = new \Namecheap\Services\DomainService($apiClient);
$domains = $domainService->getList();

// Enable sandbox mode for testing
$apiClient->enableSandbox();

// Your test code here...

// Disable sandbox mode
$apiClient->disableSandbox();