Stephane
Stephane

Reputation: 5078

WHM/Cpanel API Cron::listcron request using cURL

I'm using the code below to get a cron list but only thing I get is a blank page. Also when I replace port 2087 by 2083 the output is {"data":{"reason":"Access denied","result":"0"},"type":"text"} with a 403 HTTP status. Can you help me fix this issue?

            $user = $_ENV['REMOTE_USER'] ? $_ENV['REMOTE_USER'] : 'root';
            $accessHash = $this->getLocalAccessHash();
            if($accessHash === FALSE) return FALSE;

            $url = "https://127.0.0.1:2087/json-api/cpanel?user=$user&cpanel_jsonapi_module=Cron&cpanel_jsonapi_func=listcron&cpanel_jsonapi_version=2";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_VERBOSE, 1);
            if($fp = fopen('curl_http.txt', "wa")) curl_setopt($ch, CURLOPT_STDERR, $fp);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            //curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: WHM $user:$accessHash"));
            $cronlistJSON = curl_exec($ch);
            $ci = curl_getinfo($ch);
            curl_close($ch);
            if($fp) fclose($fp);

            print_r($ci);
            die($cronlistJSON);  

Upvotes: 2

Views: 1918

Answers (1)

vstm
vstm

Reputation: 12537

  • Check if your access hash is correct (but I guess you have already done that)
  • You can only login to root/reseller-accounts with the access hash method
  • Does your access hash maybe contain any white-space? Just strip it like that:

    $accessHash = preg_replace('/\s/', '', $accessHash);
    
  • Optional: Use the PHP cPanel-API wrapper it makes such things a lot easier.

Upvotes: 2

Related Questions