What are the advantages and disadvantages of using fsockopen() to retrieve header information in PHP?

The advantage of using fsockopen() to retrieve header information in PHP is that it allows for more control and customization compared to using built-in functions like get_headers(). However, the disadvantage is that it requires more manual coding and error handling.

// Function to retrieve header information using fsockopen()
function getHeaders($url) {
    $urlParts = parse_url($url);
    $host = $urlParts['host'];
    $port = isset($urlParts['port']) ? $urlParts['port'] : 80;
    
    $fp = fsockopen($host, $port, $errno, $errstr, 30);
    if (!$fp) {
        return "Error: $errstr ($errno)";
    } else {
        $path = isset($urlParts['path']) ? $urlParts['path'] : '/';
        fwrite($fp, "HEAD $path HTTP/1.0\r\nHost: $host\r\n\r\n");
        $headers = '';
        while (!feof($fp)) {
            $headers .= fgets($fp, 128);
        }
        fclose($fp);
        return $headers;
    }
}

// Example usage
$url = 'http://www.example.com';
$headers = getHeaders($url);
echo $headers;