loader image
View Categories

cPanel API: Creating a Sub Domain

In the world of web hosting, managing subdomains efficiently is essential for organizing and expanding your website’s structure. Subdomains can help you create distinct sections of your site, such as blog.yourdomain.com or shop.yourdomain.com. In this blog post, we’ll walk you through a PHP script designed to automate the creation of subdomains using the cPanel API. This guide is perfect for developers, system administrators, or anyone looking to streamline subdomain management.

Why Use the cPanel API? #

cPanel is a popular web hosting control panel that simplifies various aspects of domain and server management. By using the cPanel API, you can automate tasks like creating subdomains, which can save time and reduce the risk of manual errors. The API allows you to interact programmatically with cPanel, making it a powerful tool for managing your hosting environment.

Creating an API Token from cPanel #

Before using the API, you need to generate an API token in cPanel for secure authentication. Here’s how you can create one:

  1. Log in to cPanel
  2. Navigate to API Tokens:
    • Look for the “Security” section in cPanel.
    • Click on “Manage API Tokens.”
  3. Create a New API Token:
    • Click the “Create” button.
    • Enter a name for the token (e.g., “Addon Domain Script”).
  4. Copy the API Token:
    • After creating the token, copy it. You will need this token for authentication in your PHP script.

PHP Script to Create a Subdomain #

Below is a PHP script that uses the cPanel API to create a subdomain. This script demonstrates how to automate the subdomain creation process and handle the API response.

<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

function createSubdomain($subdomain, $domain) {
    $cpanelUser = 'your_cpanel_username';
    $cpanelApiToken = 'your_cpanel_api_token';
    $cpanelHost = 'your_cpanel_host';

    $query = "https://$cpanelHost:2083/json-api/cpanel?cpanel_jsonapi_user=$cpanelUser&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&domain=$subdomain&rootdomain=$domain&dir=public_html/$subdomain";

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => array(
            'Authorization: cpanel ' . $cpanelUser . ':' . $cpanelApiKey
        ),
        CURLOPT_URL => $query
    ));

    $result = curl_exec($curl);
    $error = curl_error($curl);
    $errno = curl_errno($curl);

    curl_close($curl);

    if ($error) {
        return array('error' => "cURL Error ($errno): $error");
    }

    return json_decode($result, true);
}

$subdomain = 'subdomain'; // Desired subdomain name
$rootdomain = 'itachi.pw'; // Your domain

$response = createSubdomain($subdomain, $rootdomain);

if (isset($response['error'])) {
    echo "Error creating subdomain: " . $response['error'];
} else {
    echo "Subdomain created successfully!";
}
?>

You cannot copy content of this page