amrut ratnani
amrut ratnani

Reputation: 21

Can I login to cPanel through PHP?

I want to login at cPanel through Php script and need to modified some file from the file manager.

I have cPanel username and password also but file modification I want through PHP script no by graphically.

I will use file_put_content for modified the file from the file manager.

See below code:

enter image description here

Upvotes: 2

Views: 3188

Answers (2)

jagjeet
jagjeet

Reputation: 377

Elements required to make this functionality are –

    Server/WHM Username
    Cpanel account Username
    Server login URL
    Server accesshash key

And for Accesshash key , New or already generated Access key can get from here:- WHM > Remote Access Key area and the Access Key located there. or it should be at this path “/usr/local/cpanel/bin/realmkaccesshash

Once you get all these details , you can follow the code steps as:-

            $query = "https://$server_login_link:2087/json-api/create_user_session?api.version=1&user=$cpanel_user&service=cpaneld";
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);

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

            if ($result == false) {
                // your error log
            }
            if($result){
                $decoded_response = json_decode( $result, true );
                if(isset($decoded_response['data']) && !empty($decoded_response['data'])){
                    $url = $decoded_response['data']['url'];
                    return $url;        
                }

            }        

once you get this URL, you can directly open this in a new tab or same tab and you must get logged in. It generates the similar session as cpanel login and provides you all that specific cpanel privileges.

Remeber that it only logs you in with given specific account, not for all cpanel accounts access within a server.

Upvotes: 0

Lajos Arpad
Lajos Arpad

Reputation: 76767

Yes, there is a way, CPanel has an API that can be used by PHP. Example from the docs:

// Instantiate the CPANEL object.
require_once "/usr/local/cpanel/php/cpanel.php";

// Connect to cPanel - only do this once.
$cpanel = new CPANEL();

// Get domain user data.
$get_userdata = $cpanel->uapi(
    'DomainInfo', 'domains_data',
    array(
        'format'    => 'hash',
    )
);

// Perform the desired actions.

Upvotes: 1

Related Questions