This function ignore that some header value have multiple lines...
<?php
    $connection = @imap_open("{localhost:143/imap}INBOX", "your_username", "your_password");
    $header = explode("\n", imap_fetchheader($connection, 1));
    if (is_array($header) && count($header)) {
        $head = array();
        foreach($header as $line) {
            if (eregi("^X-", $line)) {
                eregi("^([^:]*): (.*)", $line, $arg);
                $head[$arg[1]] = $arg[2];
            }
        }
    }
    ?>
I write this simple function....
$mbox = imap_open("{localhost:143/imap}INBOX", "your_username", "your_password");
$mid=1 // Message id
// Get headers
$header = imap_fetchheader($mbox, $mid);
// Split on \n
$h_array=split("\n",$header);
foreach ( $h_array as $h ) {
    // Check if row start with a char
    if ( preg_match("/^[A-Z]/i", $h )) {
        $tmp = split(":",$h);
    $header_name = $tmp[0];
    $header_value = $tmp[1];
                
    $headers[$header_name] = $header_value;
        
    } else {
        // Append row to previous field
    $headers[$header_name] = $header_value . $h;
    }
}