How can the issue of "php_network_getaddresses: gethostbyname failed" be resolved when using fsockopen in PHP?

The issue of "php_network_getaddresses: gethostbyname failed" occurs when the hostname provided to fsockopen is invalid or cannot be resolved. To resolve this issue, ensure that the hostname is correct and can be resolved by the DNS server. Additionally, check for any typos in the hostname.

$host = 'example.com';
$port = 80;

$ip = gethostbyname($host);
if ($ip) {
    $socket = fsockopen($ip, $port, $errno, $errstr, 30);
    
    if (!$socket) {
        echo "Error: $errno - $errstr";
    } else {
        // Connection successful
        fclose($socket);
    }
} else {
    echo "Invalid hostname provided.";
}