Log in to cPanel through CURL
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.
$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;
-
Hello @rccaj, We document some examples you can follow at: Guide to API Authentication - Secure Remote Logins - Developer Documentation - cPanel Documentation Guide to API Authentication - Username and Password Authentication - Developer Documentation - cPanel Documentation Can you let me know if this helps? Thank you. 0 -
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. 0
Please sign in to leave a comment.
Comments
3 comments