I'm looping through rows in my database to get information from whois results.
Here's what I have right now:
function GetEmailFromWhois($domain){
$whois = new Whois();
$query = $domain;
$whois->deep_whois=TRUE;
$result = $whois->Lookup($query, false);
$raw_data = $result["rawdata"];
$email = "";
foreach($raw_data as $item){
$items = explode(":",$item);
if($items[0] == "Registrant Email"){
$email = $items[1];
}
}
return $email;
}
The code above gets the Registrant Email
from the whois results.
I reference it later on in my code like this: $email = GetEmailFromWhois($domain);
However, at the same time as getting the registrant email, I'd also like to get the Registrant Name
, Registrant Phone
, and Registrant Country
.
I could just copy the code above 3 more times for each of those additional fields, but that would cause there to be 4 whois queries for each domain - instead of just one.
Anyone know how I can get the info I need in a single query and then use it later on in my code?
As I recently noted in another answer, parsing WHOIS data is a complex, messy affair. The exact format of WHOIS responses is not specified by any standard, and not all registries/registrars will use the format you're attempting to parse here. (Some use different labels to mark the fields you're searching for, some use labels that are ambiguous without context, some don't label certain fields at all, and some won't even include the information you're searching for here.) Worse, some registries/registrars will heavily rate limit you if it becomes apparent that you're trying to extract more than a few responses from them. In short, I'd recommend that you avoid attempting to parse WHOIS responses if at all possible.
To solve your immediate problem, though, you can create an associative array to represent the WHOIS response like so:
$arr = [];
foreach($raw_data as $item) {
list($k, $v) = explode(":", $item);
$arr[$k] = $v;
}
This will give you an associative array of the results, so you can pull out individual values using e.g.
$email = $arr["Registrant Email"];