protected _hasNetwork ( array $config = [] ) : boolean | ||
$config | array | Override the default URL to check. |
return | boolean | True if a network connection is established, false otherwise. |
protected function _hasNetwork($config = array())
{
$defaults = array('scheme' => 'http', 'host' => 'google.com');
$config += $defaults;
$url = "{$config['scheme']}://{$config['host']}";
$failed = false;
set_error_handler(function ($errno, $errstr) use(&$failed) {
$failed = true;
});
dns_check_record($config['host'], 'A');
if ($handle = fopen($url, 'r')) {
fclose($handle);
}
restore_error_handler();
return !$failed;
}