Are there any free web services available for geolocation in PHP?

There are several free web services available for geolocation in PHP, such as the ip-api.com API. This service allows you to retrieve geolocation information based on an IP address. You can make a simple HTTP request to the API and parse the JSON response to extract the desired information, such as the country, city, and coordinates.

<?php

$ip = $_SERVER['REMOTE_ADDR'];
$api_url = "http://ip-api.com/json/{$ip}";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response);

$country = $data->country;
$city = $data->city;
$latitude = $data->lat;
$longitude = $data->lon;

echo "Country: $country, City: $city, Latitude: $latitude, Longitude: $longitude";

?>