Current Location: Home> Latest Articles> How to Combine dns_get_record and GeoIP for DNS-Based Geolocation?

How to Combine dns_get_record and GeoIP for DNS-Based Geolocation?

M66 2025-06-22

In PHP development, a common method for obtaining the DNS record information of a domain is by using the dns_get_record function, while obtaining the geographical location of an IP address typically involves using the GeoIP library or a third-party API. This article will show how to combine both methods: first, retrieve the IP address via DNS query, and then use GeoIP services to obtain the geolocation based on the DNS query.


1. Using dns_get_record to Retrieve the IP Address of a Domain

dns_get_record is a built-in PHP function that returns the DNS record information for a specified domain. We can filter the A record, which is the IPv4 address associated with the domain.

Example code:

<?php
$domain = "m66.net";
<p>// Get DNS A record<br>
$dnsRecords = dns_get_record($domain, DNS_A);</p>
<p>if (!empty($dnsRecords)) {<br>
$ip = $dnsRecords[0]['ip'];<br>
echo "The IP address for domain $domain is: $ip\n";<br>
} else {<br>
echo "No A record found for domain $domain.\n";<br>
}<br>
?><br>

In the above code, we request the A record for the domain. If there are multiple records, the first IP address is selected.


2. Using GeoIP to Retrieve the Geolocation of an IP Address

Once we have the IP address, we can call a GeoIP service to obtain geolocation information. There are two common methods to achieve this:

  • Using PHP's GeoIP extension (e.g., geoip or geoip2)

  • Using third-party Web API services (such as ip-api.com, ipstack, or the Taobao IP Address Database)

Here is an example demonstrating how to get the geolocation by calling a third-party API. This example uses a hypothetical API:

<?php
$ip = "8.8.8.8"; // Example IP address
<p>// Hypothetical third-party API URL, replace with an actual working API<br>
$apiUrl = "<a rel="noopener" target="_new" class="" href="http://m66.net/api/geoip?ip=">http://m66.net/api/geoip?ip=</a>" . $ip;</p>
<p>$response = file_get_contents($apiUrl);<br>
if ($response !== false) {<br>
$data = json_decode($response, true);<br>
if ($data && isset($data['country'], $data['city'])) {<br>
echo "The geolocation for IP $ip is: " . $data['country'] . ", " . $data['city'] . "\n";<br>
} else {<br>
echo "Could not parse geolocation information.\n";<br>
}<br>
} else {<br>
echo "Failed to call GeoIP API.\n";<br>
}<br>
?><br>

Be sure to replace $apiUrl with the actual API service you're using, and be mindful of any rate limits or authorization requirements for the API.


3. Complete Example: Combining dns_get_record and GeoIP for Full Workflow

Below is the full example that combines the two steps: retrieving the IP from the domain and then querying its geolocation:

<?php
$domain = "m66.net";
<p>// 1. Get DNS A record<br>
$dnsRecords = dns_get_record($domain, DNS_A);</p>
<p>if (!empty($dnsRecords)) {<br>
$ip = $dnsRecords[0]['ip'];<br>
echo "The IP address for domain $domain is: $ip\n";</p>
$apiUrl = "http://m66.net/api/geoip?ip=" . $ip;

$response = file_get_contents($apiUrl);
if ($response !== false) {
    $data = json_decode($response, true);
    if ($data && isset($data['country'], $data['city'])) {
        echo "Geolocation: " . $data['country'] . ", " . $data['city'] . "\n";
    } else {
        echo "Could not parse geolocation information.\n";
    }
} else {
    echo "Failed to call GeoIP API.\n";
}

} else {
echo "No A record found for domain $domain.\n";
}
?>


4. Conclusion

  • dns_get_record is an effective tool in PHP for retrieving DNS information.

  • By integrating with GeoIP services, you can query the geolocation information based on the IP address.

  • In real-world applications, choose the appropriate geolocation query solution based on the service API, and be aware of the authorization and rate limit restrictions for API usage.

  • For more accurate geolocation, consider using paid services such as MaxMind's GeoIP2 database or similar options.

By using this approach, you can implement DNS-based geolocation functionality in your PHP projects, providing valuable data for website analytics, user location targeting, and other use cases.