The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with my first time with cPanel API

Discussion in 'cPanel Developers' started by Rafael Alvarez, Mar 7, 2016.

  1. Rafael Alvarez

    Joined:
    Mar 7, 2016
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    México
    cPanel Access Level:
    Reseller Owner
    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.
     
  2. cPDavidN

    cPDavidN cPanel Developer
    Staff Member

    Joined:
    Mar 19, 2013
    Messages:
    42
    Likes Received:
    3
    Trophy Points:
    8
    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
     
  3. Rafael Alvarez

    Joined:
    Mar 7, 2016
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    México
    cPanel Access Level:
    Reseller Owner
    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
     
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,723
    Likes Received:
    660
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Hello :)

    The "Single Sign On" and "Secure Remote Logins" sections of the following document might help, depending on how exactly you prefer to develop the script:

    Guide to API Authentication - Software Development Kit - cPanel Documentation

    Thank you.
     
  5. cPDavidN

    cPDavidN cPanel Developer
    Staff Member

    Joined:
    Mar 19, 2013
    Messages:
    42
    Likes Received:
    3
    Trophy Points:
    8
    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
     
  6. Rafael Alvarez

    Joined:
    Mar 7, 2016
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    México
    cPanel Access Level:
    Reseller Owner
    Thank you to both of you David and Michael, I upgraded to a VPS service. I'll give it a try.
     
  7. Rafael Alvarez

    Joined:
    Mar 7, 2016
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    México
    cPanel Access Level:
    Reseller Owner
    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;
    
    
    
    ?>
    
    
     
    #7 Rafael Alvarez, Mar 18, 2016
    Last edited by a moderator: Mar 19, 2016
  8. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,723
    Likes Received:
    660
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
Loading...

Share This Page