Help with my first time with cPanel API

Mar 7, 2016
12
2
3
México
cPanel Access Level
Root Administrator
Hello, it's my first time developing with CPanel's API and I'm having a really hard time.
I want to create a new email account from a php document, I've been reading the documentation, but I get this error "Fatal error: Class 'CPANEL' not found in..."
I don't know the exact way to import that class, I've tried more than one way to call the API, but still can't figure it out.
Can you help me please?
By the way, I only have reseller access, on hostgator.
Thanks.
 

DavidN.

Active Member
Mar 19, 2013
42
3
83
cPanel Access Level
DataCenter Provider
Hello,

Thanks for contacting us. As a reseller, it's possible that you might not be able to install files where they need to go in order to use our API classes. To add pages to cPanel, they have to go into /usr/local/cpanel/base/frontend/paper_lantern someplace, and you won't have write access there. LiveAPI only works from inside custom cPanel pages, as it requires cPanel's context to instantiate. That may be what you're running into.

To make API calls as your reseller, you will need to make HTTPS requests against the server where your reseller account lives on port 2087. There is a WHM API call, 'cpanel', which allows you to make cPanel API calls (cPanel Api1, Api2, and UAPI) on behalf of one of your users.

I hope this is helpful. If you still have questions, please feel free to ask, or open a support ticket at cPanel Customer Portal. Again, thanks for the question.

David Nielson
cPanel Developer
 
Mar 7, 2016
12
2
3
México
cPanel Access Level
Root Administrator
Thank you for answering David, now I'm trying with URL calls and it's working, however I'm obtaining the security token by loging in from the textbox and then copying the token from my browser's URL.
Is there any other way to get the security token without doing that?
Like logging in with another url call? Example: http://website.com:2083/execute/LOGIN/log_in?user=username&password=12345luggage
So then the system I'm developing can fetch the generated token and continue with other tasks.
Thanks
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,913
2,205
363

DavidN.

Active Member
Mar 19, 2013
42
3
83
cPanel Access Level
DataCenter Provider
Hello again,

If your script sets authentication headers correctly, it won't be necessary to use the security token. However, if you only have your reseller credentials and need to log into the user's account, you will need to use the create_user_session API call, then use the security token from the URL it returns for API calls as the user. You should find these documents helpful:

Guide to API Authentication - Software Development Kit - cPanel Documentation

WHM API 1 Functions - create_user_session - Software Development Kit - cPanel Documentation

David Nielson
cPanel Developer
 
Mar 7, 2016
12
2
3
México
cPanel Access Level
Root Administrator
I think I'm almost there, but I'm getting the following output with an error:

{"cpanelresult":{"apiversion":2,"error":"Sorry, the given email address is invalid.","data":[{"reason":"Sorry, the given email address is invalid.","result":0}],"func":"addpop","event":{"result":1},"module":"Email"}}

Any idea? Here's my code:

Code:
<?
$whmusername = "user";
$whmpassword = "password";
$query = 'https://127.0.0.1:2087/json-api/cpanel?cpanel_jsonapi_user=mycpaneluser&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=Email&cpanel_jsonapi_func=addpop&domain="domain.com"&user="emailuser"&password="12345luggage"&quota="500"';

$curl = curl_init();  // Create Curl Object
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);  // Allow self-signed certs
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);  // Allow certs that do not match the hostname
curl_setopt($curl, CURLOPT_HEADER,0);  // Do not include header in output
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);  // Return contents of transfer on curl_exec
$header[0] = "Authorization: Basic " . base64_encode($whmusername.":".$whmpassword) . "\n\r";
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);  // set the username and password



curl_setopt($curl, CURLOPT_URL, $query);  // execute the query
$result = curl_exec($curl);
if ($result == false) {
  error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");  
  // log error if curl exec fails
}
curl_close($curl);
print $result;



?>
 
Last edited by a moderator: