PHP
PHP Examples
Two ways to do this in PHP. The easy way (file_get_contents) and the slightly more configurable way (cURL). Both work fine.
Using file_get_contents
Get Your Own IP
<?php
$json = file_get_contents("https://whatismyip.technology/api/me");
$data = json_decode($json, true);
echo "Your IP: " . $data["ip"];
Full IP Lookup
<?php
$json = file_get_contents("https://whatismyip.technology/api/8.8.8.8");
$data = json_decode($json, true);
echo "IP: " . $data["ip"] . "\n";
echo "City: " . $data["city"] . "\n";
echo "Country: " . $data["country"] . "\n";
echo "ISP: " . $data["asn"]["name"] . "\n";
Geolocation Only
<?php
$json = file_get_contents("https://whatismyip.technology/api/8.8.8.8/geo");
$geo = json_decode($json, true);
echo $geo["city"] . ", " . $geo["country"];
Privacy Check
<?php
$json = file_get_contents("https://whatismyip.technology/api/8.8.8.8/privacy");
$privacy = json_decode($json, true);
if ($privacy["is_vpn"]) {
echo "VPN detected";
}
Using cURL
For more control over timeouts, headers, and error handling.
Full Lookup with cURL
<?php
function lookupIP(string $ip): ?array {
$ch = curl_init("https://whatismyip.technology/api/" . $ip);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
return null;
}
return json_decode($response, true);
}
$data = lookupIP("8.8.8.8");
if ($data) {
echo $data["ip"] . " is in " . $data["city"] . ", " . $data["country"];
}
Bulk Lookup with cURL
<?php
$payload = json_encode(["ips" => ["8.8.8.8", "1.1.1.1", "208.67.222.222"]]);
$ch = curl_init("https://whatismyip.technology/api/bulk");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
curl_close($ch);
$results = json_decode($response, true);
foreach ($results as $result) {
echo $result["ip"] . " -> " . $result["country"] . "\n";
}
Quick Reusable Function
<?php
function whatismyip_lookup(string $path = ""): array {
$url = "https://whatismyip.technology/api/me" . ($path ? "/" . $path : "");
$json = @file_get_contents($url);
if ($json === false) {
throw new RuntimeException("API request failed");
}
return json_decode($json, true);
}
// Your own IP
$me = whatismyip_lookup();
echo "My IP: " . $me["ip"];
// Any IP, full lookup
$google = whatismyip_lookup("8.8.8.8");
echo $google["city"];
// Just geo
$geo = whatismyip_lookup("8.8.8.8/geo");
echo $geo["country"];
That covers PHP. Pick whichever method fits your project. If you’re in a framework like Laravel, file_get_contents is usually fine. If you need retry logic or custom headers, go with cURL.