Skip to content

Instantly share code, notes, and snippets.

@iMaz1n
Created October 17, 2021 10:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iMaz1n/ccd6bd1f6d34839918c6420330b7549e to your computer and use it in GitHub Desktop.
Save iMaz1n/ccd6bd1f6d34839918c6420330b7549e to your computer and use it in GitHub Desktop.
Use API/Curl to create a new subdomain in both CloudFlare and cPanel
<?php
/**
* Create Subdomains
*
* Note: This script was designed to be called silently by a bash script for project creation.
* Add to bash script: php "$HOME/scripts/php/create-subdomains.php" "$new_account_subdomain"
*
* Requires "jamesryanbell/cloudflare": "^1.11" for the CloudFlare PHP library
* Recommends "kint-php/kint": "^3.2" for improved temporary PHP debugging
*
* @author Darren Sopiarz <dsthedev@gmail.com>
* @todo pass in bash variables from main script and verify that everything all works together
*/
// Load Composer
require_once dirname(__FILE__) . '/../../vendor/autoload.php';
// This grabs the first argument being passed in from a bash script that calls this script
$new_subdomain = $argv[1];
// $new_subdomain = 'noselfsigned';
if ('' == $new_subdomain || empty($new_subdomain)) {
die("No subdomain entered.\n");
}
/**
* create_cloudflare_subdomain
*
* @todo Move the credentials into a safer folder on the server and include it above to gain secure access
* @todo Find a way to add a check to see if it fails, and pass a message back into calling bash script
*/
function create_cloudflare_subdomain($subdomain = '')
{
// Cloudflare Credentials
$email = 'user@email.com';
$auth_key = 'getfromcloudflareaccount';
$zone_id = 'getfromcloudflareaccount';
// New Record Information
$record_type = 'A';
$servers_ip = 'XXX.XX.XX.XX';
$ttl = null;
$proxied = false;
// 1. Connect to CloudFlare
$CFAPI = new \Cloudflare\Api($email, $auth_key);
// 2. Instantiate the DNS Class for CloudFlare.
$CFDNS = new \Cloudflare\Zone\Dns($CFAPI);
// 3. Create the DNS Record
$CFDNS->create($zone_id, $record_type, $subdomain, $servers_ip, $ttl, $proxied);
}
create_cloudflare_subdomain($new_subdomain);
/**
* create_new_cpanel_subdomain
*
* @link https://stackoverflow.com/questions/31534861/how-to-create-subdomains-using-curl#answer-31542859
*/
function create_new_cpanel_subdomain($subdomain = '')
{
$cpanel_user = 'username';
$rootdomain = 'sub.domain.com';
$whm_user = 'root';
$whm_port = '2087';
$whm_pass = 'superstrongpassword';
$doc_root = '%2Fprojects%2F' . $subdomain . '%2Fpublic_html';
$header[0] = "Authorization: Basic " . base64_encode($whm_user . ":" . $whm_pass) . "\n\r";
$result = '';
$query = 'https://' . $rootdomain . ':' . $whm_port . '/json-api/cpanel?cpanel_jsonapi_user=' . $cpanel_user
. '&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&domain='
. $subdomain . '&rootdomain=' . $rootdomain . '&dir=' . $doc_root;
$curl_handle = curl_init(); // Create Curl Object
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, 1); // Don't allow self-signed certs?
// curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 0); // Allow certs that do not match the hostname
curl_setopt($curl_handle, CURLOPT_HEADER, 0); // Do not include header in output
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); // Return contents of transfer on curl_exec
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $header); // set the username and password
curl_setopt($curl_handle, CURLOPT_URL, $query); // execute the query
$result = curl_exec($curl_handle);
if ($result == false) {
error_log("curl_exec threw error \"" . curl_error($curl_handle) . "\" for $query");
}
curl_close($curl_handle);
}
create_new_cpanel_subdomain($new_subdomain);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment