Whm/cpanel api example

https://www.codepunker.com/blog/using-php-to-create-new-subdomains-databases-and-email-accounts-on-a-cpanel-driven-server

To achieve our goal, we’ll use Composer – the PHP dependency manager and the mgufrone/cpanel-whm. package open sourced by Mochamad Gufron

Let’s start by cloning the package into a new folder on a WHM server.

To test it out, in the root folder of your project create a new PHP file, require vendor/autoload and try listing the cPanel accounts on your server.

 

If you received a JSON string with all cPanel accounts from your server, then the authentication process worked perfectly.

An important aspect when working with the WHM API is that there are different versions of it, so you need to figure out which one is best for whatever it is that you want to accomplish.

WHM has cPanel API 1 (the version I was using in the original article - from 2014)cPanel API 2 (newer but now deprecated) and UAPI (the latest one).

The gufy/cpanel-whm package provides an interface for interacting with either of these APIs so let’s get back to our file and define some constants for each. Then we will create a new database under one of our cPanel accounts. One more thing to remember is that if you use database prefixes on your instance, you’ll need to pass that prefix too.

 

Next step is to create a new user and grant him access to do anything with that database – GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'%' WITH GRANT OPTION;

 

To create new email accounts, make use of the add_pop function.

 

To create new sub-domains on an existing account, UAPI doesn’t yet have an implementation so we will use the API Version 2 in order to achieve the same result.

 

Hope you find this useful! Let me know your thoughts in the comments area

What follows is the original article from 2014. Please consider using the updated version above.

Many hosting providers use cPanel and this tutorial comes as a helper for those of you who need to manage various server aspects with PHP. I will show you how to create new sub-domains, databases and email accounts on a cPanel driven server, from your PHP code. In order to be able to talk back and forth with the cPanel API you’ll need the cPanel XMLAPI Client Class which you can find on GitHub

Creating a new sub-domain

 

Creating a new database and a user for it

 

Adding a new email account

 

4 ماه پیش

پاسخ دهید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *