Get errorlog with php script

Dirgni

New member
Joined
Dec 24, 2025
Messages
3
I have a function that I use to get the errorlog from the DirectAdmin errorlog. This worked fine for several years. Now my application is moved to another server with a newer version of DirectAdmin and my script doesn't return the any result, although I see the errorlog isn't empty. Should there be changed something or is there a better way to get the errorlog?

PHP:
<?php
    function getErrorlog($sServer, $sPort, $sUsername, $sPassword, $sDomain, $sSubdomain) {
        // get the errorlog from direct admin
        $url = 'https://' . $sServer . ':' . $sPort;
        $ckfile = tempnam ('/tmp', 'CURLCOOKIE');
        $fields = array(
            'referer' => urlencode('/'),
            'username' => urlencode($sUsername),
            'password' => urlencode($sPassword)
        );
        $fields_string = '';
        foreach($fields as $key => $value) {
            $fields_string .= $key . '=' . $value . '&';
        }
        rtrim($fields_string, '&');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url . '/CMD_LOGIN');
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Referer: ' . $url . '/CMD_LOGIN'));
        $result = curl_exec($ch);
        if($result === false) {
            die('CURL ERROR: ' . curl_error($ch));
        }
        else {
            curl_setopt($ch, CURLOPT_URL, $url . '/CMD_SHOW_LOG?domain=' . $sDomain . '&type=error&subdomain=' . $sSubdomain);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $result = curl_exec($ch);
            curl_close($ch);
            if($result === false) {
                die('CURL ERROR: ' . curl_error($ch));
            }
            else {
                $aLines = explode(PHP_EOL, $result);
                $sResult = '';
                foreach ($aLines as $line) {
                    if ($line != '') {
                        $iPosCertificate = strpos($line, 'server certificate does NOT include an ID which matches the server name');
                        $iPosEnv = strpos($line, '/.env');
                        $iPosStatus = strpos($line, '/server-status');                     
                        if ($iPosCertificate == 0 && $iPosEnv == 0 && $iPosStatus == 0) {
                            $sResult .= $line . PHP_EOL . PHP_EOL;
                        }
                    }
                }
                return $sResult;
            }
        }
    }
?>
 
Hello,

What error do you get when running the function on a new server?

You might temporary change the lines

PHP:
$result = curl_exec($ch);

to

PHP:
$result = curl_exec($ch);
var_dump($result);

to see output from API calls.
 
Hello,

What error do you get when running the function on a new server?

You might temporary change the lines

PHP:
$result = curl_exec($ch);

to

PHP:
$result = curl_exec($ch);
var_dump($result);

to see output from API calls.
I get no error, I just get no result.
 
I've used the API example script now and then I get 'string(37) "The requested URL returned error: 401"' with var_dump(curl_error) and no result.

PHP:
$server = 'https://' . DA_SERVER . ':' . DA_PORT;
$user = DA_USER;
$password = DA_PASSWORD;
$endpoint = '/api/db-show/databases';;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $server . $endpoint);
curl_setopt($curl, CURLOPT_USERPWD, $user . ":" . $password);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
$response_json = curl_exec($curl);
curl_close($curl);
var_dump(curl_error($curl));
$data = json_decode($response_json, true);
foreach ($data as $item){
    if (isset($item['database'])) {
        echo "Database: {$item['database']}\n";
    };
}

If I change the endpoint to '/CMD_SHOW_LOG' var_dump(curl_error) returns no error but also no result

string(0) ""
Warning: foreach() argument must be of type array|object, null given in /home/domains/domain.nl/private/cronjob/errorlog.php on line 24.

What am I doing wrong?
 
Back
Top