Skip to content

Instantly share code, notes, and snippets.

@Grendel7
Last active November 22, 2023 11:36
Show Gist options
  • Save Grendel7/acdb9075a2b5da1ad176591125f9f39b to your computer and use it in GitHub Desktop.
Save Grendel7/acdb9075a2b5da1ad176591125f9f39b to your computer and use it in GitHub Desktop.
Test connection to a hostname and port with PHP
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
$domain = "smtp.gmail.com";
$port = 587;
if ($socket = stream_socket_client("tcp://{$domain}:{$port}", $errno, $errstr, 10)) {
echo "stream_socket_client: Connection to {$domain}:{$port} successful!<br>";
} else {
echo "stream_socket_client error ({$errno}): {$errstr}<br>";
}
if (fsockopen($domain, $port)) {
echo "fsockopen: Connection to {$domain}:{$port} successful!<br>";
} else {
echo "fsockopen error: ".error_get_last()['message']."<br>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment