This works:
$connection = fsockopen("whois.iis.se", 43);
fputs($connection, "google.se\r\n");
while (!feof($connection)) {
$data .= fgets($connection, 4096);
}
fclose($connection);
echo nl2br($data);
But this do not work:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "whois.iis.se");
curl_setopt($ch, CURLOPT_PORT, 43);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "google.se\r\n");
$data = curl_exec($ch);
curl_close($ch);
echo nl2br($data);
What is wrong with this curl function?
The WHOIS service protocol is much older than HTTP, dating from the mid 1980s, and has little to nothing in common with it. You can only use curl
on HTTP-type connections, not arbitrary services.
I know this is a year old but: The accepted answer is incorrect. cURL actually did have functionality to support the Whois protocol but it takes a bit more than a simple request.
I do not know the exact translation to PHP but I think this would just need options changed to support TELNET since that is what Whois uses:
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_TELNET);
Finally once you get a response back, you may need to deal with referrals unless you are hitting the direct Whois server that has the actual WhoIs data. For the Whois server in the example it should return mot addresses as as the response for google.se
.
This support has been in it for a long time now, I believe even before this question was asked.
UPDATE:
Sorry, haven't really worked with PHP much and PHP's CURL but got the following to work. It isn't ideal but wasn't sure best way to handle the stdin stuff.
$filename = "query.txt";
$fp = fopen($filename, "r");
function readFunc($ch, $fh, $length = false) {
return fread($fh, $length);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "telnet://whois.iis.se:43");
curl_setopt($ch, CURLOPT_PORT, 43);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_NOPROGRESS, TRUE);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_TELNET);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filename));
curl_setopt($ch, CURLOPT_READFUNCTION, 'readFunc');
curl_setopt($ch, CURLOPT_VERBOSE, TRUE); // Can take this out
$data = curl_exec($ch);
curl_close($ch);
echo nl2br($data);
This then returns:
$ php query.php
* Rebuilt URL to: telnet://whois.iis.se:43/
* Trying 91.226.37.83...
* TCP_NODELAY set
* Connected to whois.iis.se (91.226.37.83) port 43 (#0)
* Closing connection 0
# Copyright (c) 1997- IIS (The Internet Foundation In Sweden).<br />
# All rights reserved.<br />
# The information obtained through searches, or otherwise, is protected<br />
# by the Swedish Copyright Act (1960:729) and international conventions.<br />
# It is also subject to database protection according to the Swedish<br />
# Copyright Act.<br />
# Any use of this material to target advertising or<br />
# similar activities is forbidden and will be prosecuted.<br />
# If any of the information below is transferred to a third<br />
# party, it must be done in its entirety. This server must<br />
# not be used as a backend for a search engine.<br />
# Result of search for registered domain names under<br />
# the .se top level domain.<br />
# This whois printout is printed with UTF-8 encoding.<br />
#<br />
state: active<br />
domain: google.se<br />
holder: mmr8008-53808<br />
admin-c: -<br />
tech-c: -<br />
billing-c: -<br />
created: 2008-10-20<br />
modified: 2016-09-18<br />
expires: 2017-10-20<br />
transferred: 2009-03-06<br />
nserver: ns1.google.com<br />
nserver: ns2.google.com<br />
nserver: ns3.google.com<br />
nserver: ns4.google.com<br />
dnssec: unsigned delegation<br />
status: serverDeleteProhibited<br />
status: serverTransferProhibited<br />
status: serverUpdateProhibited<br />
registrar: MarkMonitor Inc<br />
If you need it to work for random strings, then you can easily just write to a temporary file and use that to pass into the function. I unfortunately do not have the time to continue digging through the PHP curl options to determine which option may work best for streaming the data to the WHOIS server.
The file query.txt
just has google.se
in it.
来源:https://stackoverflow.com/questions/44008993/whois-works-with-fsockopen-not-with-curl