/** * Wrapper around Jeremy Kendall's PHP Domain Parser that parses the * domain/url passed to the function and returns the Tld and Valid domain * @param $domain * @throws \InvalidArgumentException when the tld is not valid * @return array returns an associative array with the domain and tld */ private function parse($domain) { $pslManager = new PublicSuffixListManager(); $parser = new Parser($pslManager->getList()); // First check if the suffix is actually valid if (!$parser->isSuffixValid($domain)) { throw new \InvalidArgumentException("Invalid TLD"); } $components = []; $components["tld"] = $parser->getPublicSuffix($domain); $components["domain"] = $parser->getRegisterableDomain($domain); return $components; }
public function getLiveDns($hostname) { $pslManager = new PublicSuffixListManager(); $domainParser = new Parser($pslManager->getList()); $hostname = strtolower($hostname); $baseDomain = $domainParser->getRegisterableDomain($hostname); $ipUtils = $this->ipUtils; $records = Cache::remember($hostname, 60 * 24, function () use($ipUtils, $hostname) { $dns = new Dns(['8.8.8.8', '8.8.4.4', 2]); $records = $dns->getDomainRecords($hostname, $testNameserver = false); ksort($records); if (isset($records['A']) === true) { $records['A'] = array_unique($records['A']); foreach ($records['A'] as $key => $address) { $geoip = $ipUtils->geoip($address); if ($geoip->country->isoCode) { $country_code = $geoip->country->isoCode; $country_name = $geoip->country->name; $city_name = $geoip->city->name; } else { $ipDec = $this->ipUtils->ip2dec($address); $prefix = IPv4BgpPrefix::where('ip_dec_start', '<=', $ipDec)->where('ip_dec_end', '>=', $ipDec)->orderBy('cidr', 'asc')->first(); if ($prefix && ($prefixWhois = $prefix->whois())) { $country_code = $prefixWhois->counrty_code; $country_name = $prefixWhois->counrty_code ? trans('countries.' . $prefixWhois->counrty_code) : null; $city_name = null; } else { $country_code = null; $country_name = 'Unknown'; $city_name = null; } } $output['address'] = $address; $output['country_code'] = $country_code; if ($city_name) { $output['location'] = $city_name . ', ' . $country_name; } else { $output['location'] = $country_name; } $records['A'][$key] = $output; } } if (isset($records['AAAA']) === true) { $records['AAAA'] = array_unique($records['AAAA']); foreach ($records['AAAA'] as $key => $address) { $geoip = $ipUtils->geoip($address); if ($geoip->country->isoCode) { $country_code = $geoip->country->isoCode; $country_name = $geoip->country->name; $city_name = $geoip->city->name; } else { $ipDec = $this->ipUtils->ip2dec($address); $prefix = IPv6BgpPrefix::where('ip_dec_start', '<=', $ipDec)->where('ip_dec_end', '>=', $ipDec)->orderBy('cidr', 'asc')->first(); if ($prefix && ($prefixWhois = $prefix->whois())) { $country_code = $prefixWhois->counrty_code; $country_name = $prefixWhois->counrty_code ? trans('countries.' . $prefixWhois->counrty_code) : null; $city_name = null; } else { $country_code = null; $country_name = 'Unknown'; $city_name = null; } } $output['address'] = $address; $output['country_code'] = $country_code; if ($city_name) { $output['location'] = $city_name . ', ' . $country_name; } else { $output['location'] = $country_name; } $records['AAAA'][$key] = $output; } } return $records; }); $data['hostname'] = $hostname; $data['base_domain'] = $baseDomain; $data['dns_records'] = $records; return $this->sendData($data); }
// Obtain an instance of the parser $pslManager = new PublicSuffixListManager(); $parser = new Parser($pslManager->getList()); // Parse a URL $url = $parser->parseUrl('http://*****:*****@www.pref.okinawa.jp:8080/path/to/page.html?query=string#fragment'); // Accessing elements of the URL var_dump($url); var_dump($url->__toString()); var_dump($url->path); var_dump($url->fragment); // Getting the Host object from the URL $host = $url->host; // Accessing elements of the Host var_dump($host); var_dump($host->__toString()); var_dump($host->subdomain); var_dump($host->registerableDomain); var_dump($host->publicSuffix); // It's possible to parse a host only, if you prefer $host = $parser->parseHost('a.b.c.cy'); // Accessing elements of the Host var_dump($host); var_dump($host->__toString()); var_dump($host->subdomain); var_dump($host->registerableDomain); var_dump($host->publicSuffix); // If you just need to know subdomain/registerable domain/public suffix info // about a host, there are public methods available for that in the Parser var_dump($parser->getSubdomain('www.scottwills.co.uk')); var_dump($parser->getRegisterableDomain('www.scottwills.co.uk')); var_dump($parser->getPublicSuffix('www.scottwills.co.uk'));