Is there a sort of reliable way to programmatically create MySQL databases and MySQL users on shared Linux hosting plans from a PHP page? For instance, accessing cpanel or phpmyadmin through an API or hook if I know my cpanel access information and/or my FTP information?
Occasionally I see this with some sites and products but don’t know how they do it.
Thanks to @Alex C who got me on the right track. The following will work with many shared hosting plans, but you’ll want to check with their policies on this first. Mine wrote me back in a tech support ticket and said, “You have to automate through a cpanel script.” So, that’s what this is.
In the example below, I would have already purchased root.com as my main root domain of my shared hosting plan. I would be wanting to setup a database named “user_myexample” with password “myexample” assigned with full privileges to user “user_myexamp”. If the “user_” prefix and “myexamp” looks odd — it’s because cpanel has a prefix based on the root user’s user account into cpanel, and the database user name can only be 7 characters maximum.
To connect to Cpanel to make this happen, I entered a cpanel homepage URL (which varies with hosting plan) so that it could be parsed and reused. Also, I provided my root.com’s cpanel user/pass information.
The echo statements are just the output response whether each HTTP GET request worked or not. You may want to inspect that to see if there’s something you can parse out of that output for success/failure.
Note some hosting plans block file_get_contents connecting to a URL, so you may have to switch with fopen($sURL, ‘r’) or Curl API.