{"cpanelresult":{"apiversion":"2","error":"Access denied","data":{"reason":"Access denied","result":"0"},"type":"text"}}

Operating System & Version
CLOUDLINUX 7.7
cPanel & WHM Version
v84.0.21

Matt Paplham

Member
Apr 12, 2019
11
3
3
WI - USA
cPanel Access Level
Root Administrator
Hi there,

I have a Gravity Form on a Wordpress website. The goal is once it is filled out, it will create a cPanel account (along with other things).

I'm using gform_confirmation to hook into the form data.

Everything is working until I call WHM API 1 - Create an account. Then I get the error {"cpanelresult":{"apiversion":"2","error":"Access denied","data":{"reason":"Access denied","result":"0"},"type":"text"}}.

The specific code is below:

PHP:
    public function create_account($cp_user) { 
        print 'Creating cPanel Account...';
        $this->cp_user = $cp_user;
        $url = "$this->server_address:2087/json-api/createacct?api.version=1&username=$this->cp_user&domain=$this->domain&contactemail=$this->email&dkim=1&frontpage=0&hasshell=1&language=en&owner=root&mxcheck=auto&owner=root&password=$this->password&plan=Inception";
        $header[0] = "Authorization: whm root:$this->server_auth";
        $ch = curl_init();
        $options = array(
            CURLOPT_URL => $url,
            URLOPT_RETURNTRANSFER => 1,
            CURLOPT_HTTPHEADER => $header);
        curl_setopt_array($ch, $options);
        
        
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        curl_close($ch);
        
    }
I've double check the authorization token and even increased permissions to everything to rule that out as a problem.
I've tried on two different servers and both are giving the same problem.
I've tried with an api token I know works from the command line.
I've copied and pasted the code from Guide to API Authorization, then edited it to my values. Still same issue.
I've printed out $url and $header to make sure the correct values are there.
I've tried the curl to PHP converter. Still same issue.

Anyone have a suggestion on what I am doing wrong?
 

cPanelTJ

Product Owner
Staff member
Jan 29, 2019
97
48
93
Houston, TX
cPanel Access Level
Root Administrator
Twitter
Hi @Matt Paplham ,

I'm not seeing any issues with API Token authorization on my end. I've tested it for creating an account and list accounts through Perl, PHP, and curl. You said you tried an API token that you know works from command line; is that using curl with the same createacct endpoint?

Do the servers that you are testing on have a registered SSL certificate? If not, you'll need
CURLOPT_SSL_VERIFYHOST,0 in your header of your PHP code and --insecure in your curl over CLI.
 
  • Like
Reactions: cPanelLauren

Matt Paplham

Member
Apr 12, 2019
11
3
3
WI - USA
cPanel Access Level
Root Administrator
Thanks for your response. I appreciate your time.

From the command line, I am using the same endpoint, yes. And yes, our servers have a registered SSL certificate.

With a fresh mind this morning I have figured out it is something with how I've set up the variables. If I set the curl options using no variables, it works just fine.
 
  • Like
Reactions: cPanelLauren

Matt Paplham

Member
Apr 12, 2019
11
3
3
WI - USA
cPanel Access Level
Root Administrator
I changed the following and it worked:

PHP:
$url = $this->server_address.':2087/json-api/createacct?api.version=1&username='.$this->cp_user.'&domain='.$this->domain.'&contactemail='.$this->email.'&dkim=1&frontpage=0&hasshell=1&language=en&owner=root&mxcheck=auto&owner=root&password='.$this->password.'&plan=Inception';
$header[0] = 'Authorization: whm root:'.$this->server_auth;
I'm fairly new to PHP, but I thought either way would work. At any rate, thanks for all your help.
 
  • Like
Reactions: cPanelLauren