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!

Log in to cPanel through CURL

Discussion in 'cPanel Developers' started by rccaj, Dec 12, 2018.

  1. rccaj

    rccaj Member

    Joined:
    Jun 16, 2017
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    India
    cPanel Access Level:
    Root Administrator
    I am using above script to login through cpanel using curl. But it always gives login invalid. Neither it creates cookie file on path given.

    Any Idea how to connect through cpanel using curl or any settings that need to be done.

    I do not have whm login as I am on shared hosting.

    Code:
    $cp_user = "$cpanel_user";
    $cp_pwd = "$cpanel_passwd";
    $url = "https://example.com:2083/login";
    $cookies = "/path to save your cookie/cookies.txt";
    
    
    // Create new curl handle
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies );
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); // Save cookies to
    curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$cp_user&pass=$cp_pwd");
    curl_setopt($ch, CURLOPT_TIMEOUT, 100020);
    
    // Execute the curl handle and fetch info then close streams.
    $f = curl_exec($ch);
    $h = curl_getinfo($ch);
    //curl_close($ch);
    //$result = curl_exec($curl);
        if ($f == false) {
            error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
                                                            // log error if curl exec fails
            //print_r("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
        }
        
        // print_r($f);
    if ($f == true and strpos($h['url'],"cpsess"))
    {
        // Get the cpsess part of the url
        $pattern="/.*?(\/cpsess.*?)\/.*?/is";
        $preg_res=preg_match($pattern,$h['url'],$cpsess);
    }
    $token=$cpsess[1];
    curl_close($ch);
    return $token;
    
    
     
    #1 rccaj, Dec 12, 2018
    Last edited by a moderator: Dec 12, 2018
  2. cPanelMichael

    cPanelMichael Technical Support Community Manager Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    47,555
    Likes Received:
    2,182
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
  3. rccaj

    rccaj Member

    Joined:
    Jun 16, 2017
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    India
    cPanel Access Level:
    Root Administrator
    Hi @cPanelMichael I am looking for solution where i am just on shared hosting and can validate cpanel token key and get logged in through curl and my php script could work and this is mainly for creating aliases.
     
  4. cPanelMichael

    cPanelMichael Technical Support Community Manager Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    47,555
    Likes Received:
    2,182
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
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