Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

cPanel Login via api

Discussion in 'cPanel Developers' started by charan315, Oct 30, 2016.

  1. charan315

    charan315 Registered

    Joined:
    Sep 5, 2012
    Messages:
    2
    Likes Received:
    1
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hi,

    I want users to be able to login from my website to cpanel without giving username or password like in whmcs.

    I tried the below code from: Guide to API Authentication - Software Development Kit - cPanel Documentation

    I'm able to get 2 urls from api:
    10.0.0.1:2083/cpsess5394331821/login/?session=username%3aD3SDQS2C8zc4Hkqe%3acreate_user_session%2cd4c7ca8292935fc1a0f290c0710d792e

    10.0.0.1:2083/cpsess5394331821/frontend/paper_lantern/index.html?login=1&post_login=20281846205496

    but when I redirect neither of them logs the user into cpanel(goes to login page).
    Can anyone please point me in the right direction.

    Thanks.

    PHP:
    <?php
    $whmusername 
    "whmuser";
    $cpanel_user "cpaneluser";
    # The contents of /root/.accesshash
    $hash "";

    $query "https://10.0.0.1:2087/json-api/create_user_session?api.version=1&user=$cpanel_user&service=cpaneld";

    $curl curl_init();
    curl_setopt($curlCURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($curlCURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($curlCURLOPT_RETURNTRANSFER,1);

    $header[0] = "Authorization: WHM $whmusername:" preg_replace("'(\r|\n)'","",$hash);
    curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
    curl_setopt($curlCURLOPT_URL$query);

    $result curl_exec($curl);

    if (
    $result == false) {
      
        
    error_log("curl_exec threw error \"" curl_error($curl) . "\" for $query");
    }


    $decoded_response json_decode$resulttrue );
    $session_url $decoded_response['data']['url'];
    $cookie_jar 'cookie.txt';
    print_r($session_url);
    curl_setopt($curlCURLOPT_HTTPHEADERnull);             // Unset the authentication header.
    curl_setopt($curlCURLOPT_COOKIESESSIONtrue);          // Initiate a new cookie session.
    curl_setopt($curlCURLOPT_COOKIEJAR$cookie_jar);       // Set the cookie jar.
    curl_setopt($curlCURLOPT_COOKIEFILE$cookie_jar);      // Set the cookie file.
    curl_setopt($curlCURLOPT_URL$session_url);            // Set the query url to the session login url.

    $result curl_exec($curl);                               // Execute the session login call.
    if ($result == false) {
        
    error_log("curl_exec threw error \"" curl_error($curl) . "\" for $query");
                                                        
    // Log an error if curl_exec fails.
    }
    echo 
    $session_url;



    print_r(str_replace("URL=/","URL=https://10.0.0.1:2083/",$result));


    curl_close($curl);

    ?>
     
    #1 charan315, Oct 30, 2016
    Last edited by a moderator: Oct 30, 2016
    urgido likes this.
  2. charan315

    charan315 Registered

    Joined:
    Sep 5, 2012
    Messages:
    2
    Likes Received:
    1
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Never mind the above code works we can just redirect to $session_url.
     
  3. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    44,803
    Likes Received:
    1,897
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hello,

    I'm happy to see you were able reach a solution. Thank you for updating us with the outcome.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice