Skip to content

Instantly share code, notes, and snippets.

@johnroyer
Last active September 22, 2023 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnroyer/8d27be240f972a505cd53a27a461588f to your computer and use it in GitHub Desktop.
Save johnroyer/8d27be240f972a505cd53a27a461588f to your computer and use it in GitHub Desktop.
whois query using TCP socket in PHP
<?php
error_reporting(E_ALL);
echo "connection to whois.twnic.net.tw\n";
$address = gethostbyname('whois.iana.org.');
$fp = @fsockopen($address, 43, $errno, $errstr, 5) or die("Socket Error " . $errno . " - " . $errstr);
stream_set_blocking($fp, true);
echo "socket openned\n";
// whois protocol RFC
// https://www.rfc-editor.org/rfc/rfc3912.txt
echo "sending request ... ";
$query = 'zeroplex.tw' . "\r\n";
fputs($fp, $query);
echo "ok \n";
echo "closing socket ... ";
fclose($fp);
echo "ok\n";
$refer = preg_match('/refer: +([^ ]+)/', $result, $matches);
if (1 === $refer) {
echo "refer found: ";
var_dump($matches);
} else {
echo "--------------- \n";
echo $result . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment