To create an API for
cPanel, you can leverage the
cPanel UAPI or
cPanel API 2 to automate tasks such as DNS management (SPF, DKIM, DMARC), managing emails, or interacting with server settings.
Example: Basic API to Add DNS Records (SPF, DKIM, DMARC)
1. PHP Script to Call cPanel API:
<?php
$cpanel_user = 'your_cpanel_user';
$cpanel_password = 'your_cpanel_password';
$cpanel_host = 'your_cpanel_host.com';
// API URL
$api_url = "https://$cpanel_host:2083/json-api/cpanel";
// SPF, DKIM, and DMARC DNS Records
$domain = 'example.com';
$spf_value = "v=spf1 include:_spf.example.com -all";
$dkim_value = "v=DKIM1; k=rsa; p=public_key_here";
$dmarc_value = "v=DMARC1; p=reject; rua=mailto:[email protected]";
// Function to make API request
function make_api_request($cpanel_user, $cpanel_password, $domain, $record_type, $value) {
$url = "https://$cpanel_host:2083/json-api/cpanel?user=$cpanel_user&module=DNS&domain=$domain&record=$record_type&value=" . urlencode($value);
$response = file_get_contents($url);
return $response;
}
// Add DNS records (SPF, DKIM, DMARC)
add_dns_record($domain, 'TXT', $spf_value);
add_dns_record($domain, 'TXT', $dkim_value);
add_dns_record($domain, 'TXT', $dmarc_value);
echo "DNS records added successfully!";
?>
2. Explanation:
- The script connects to cPanel’s JSON API to manage DNS records.
add_dns_record()
adds a TXT record for SPF, DKIM, or DMARC.
- The
make_api_request()
function sends a request to the cPanel API to manage DNS records for the specified domain.
3. Security Considerations:
- Ensure SSL/TLS is enabled (
https://
) for secure API calls.
- Consider implementing authentication via cPanel API tokens rather than embedding the password directly.
Additional Features:
You can expand this API to:
- Add headers to Apache configurations (via
/etc/httpd/conf/httpd.conf
).
- Manage email settings, databases, or SSL certificates.