Skip to main content

PHP : how to get the used space by an email account ?

Comments

8 comments

  • cPRex Jurassic Moderator
    Hey there! If you want to use PHP, it might be best to use an API call to get that disk usage. We have details on how to use that API call here:
    0
  • trucmuche
    Hello, Thank you very much for your help. But I didn't managed to make it run as I want. The method described in LiveAPI PHP tab asks to create a file "Email_get_disk_usage.live.php" and put some code in it, then use an established CPANEL session to display needed informations. That works, ok, but for my application, I need to integrate the API call to my PHP code and get the email usage without being logged in CPanel. You can imagine that I want that : [CODE=php]uapi( 'Email', 'get_disk_usage', array ( 'user' => $user, 'domain' => $domain, ) ); // Handle the response if ($response['cpanelresult'>['result'>['status'>) { $data = $response['cpanelresult'>['result'>['data'>; // Do something with the $data } else { // Report errors and do things } // Disconnect from cPanel - only do this once. $cpanel->end(); ?>
    How can I do ? Thanks, T.
    0
  • cPRex Jurassic Moderator
    Thanks for that additional clarification. For your situation, you'll want to use API tokens:
    0
  • trucmuche
    Thanks ! I created token and checked "initial privileges" and I managed to auth & run the query "Return email account's disk usage " cPanel & WHM Developer Portal) but can't find what's wrong... Here is my full code : $user = CPANEL_TOKEN_NAME; // defined using define() $token = CPANEL_TOKEN_KEY; // defined using define() //$query = "https://127.0.0.1:2087/json-api/listaccts?api.version=1"; // works $query = 'https://127.0.0.1:2087/execute/Email/get_disk_usage?user=username@mydomain.com&domain=mydomain.com'; $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 $user:$token"; curl_setopt($curl,CURLOPT_HTTPHEADER,$header); curl_setopt($curl, CURLOPT_URL, $query); $result = curl_exec($curl); $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($http_status != 200) { echo "[!] Error: " . $http_status . " returned\n"; } else { $json = json_decode($result); echo "
    ";
            print_r($json);
            echo "
    "; } curl_close($curl);

    0
  • trucmuche
    Anybody to explain why the query [CODE=php]$query = 'https://127.0.0.1:2087/execute/Email/get_disk_usage?user=username@mydomain.com&domain=mydomain.com';
    returns 404 but [CODE=php]$query = "https://127.0.0.1:2087/json-api/listaccts?api.version=1";
    works ? How should I call "Email/get_disk_usage" ? I can't find the information in the developer help center... Thanks for your help ! T.
    0
  • cPRex Jurassic Moderator
    That API call is actually at the user level, so you'd want to use port 2083 with a user session, instead of port 2087 as root. Can you tweak that and see if that gets things working?
    0
  • trucmuche
    That API call is actually at the user level, so you'd want to use port 2083 with a user session, instead of port 2087 as root. Can you tweak that and see if that gets things working?

    GOT IT ! :-) I created the token using WHM and logged as root with the first request. Now I created a token in the user account (CPanel) and use it in my code ; it works ! Many thanks !!!!
    0
  • cPRex Jurassic Moderator
    Great - I'm glad to hear that's working well!
    0

Please sign in to leave a comment.