### Install Laravel Project Source: https://freeipapi.com/blog/posts/how-to-use-freeipapi-in-your-laravel-project-with-caching Use Composer to create a new Laravel project and start the development server. ```bash composer create-project laravel/laravel freeipapi-demo cd freeipapi-demo php artisan serve ``` -------------------------------- ### Get IP Info with Java Source: https://freeipapi.com/ A Java example demonstrating how to fetch IP information using HttpURLConnection. Remember to substitute {IP-ADDRESS} with the actual IP. ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class Main { public static void main(String[] args) throws IOException { String ipAddress = "{IP-ADDRESS}"; String urlString = "https://free.freeipapi.com/api/json/" + ipAddress; URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); System.out.println(response.toString()); } } ``` -------------------------------- ### Get IP Info with C# Source: https://freeipapi.com/ This C# example uses HttpClient to asynchronously fetch IP address information. Replace {IP-ADDRESS} with the target IP. ```csharp using System; using System.Net.Http; using System.Threading.Tasks; class Program { static async Task Main() { string ipAddress = "{IP-ADDRESS}"; string url = $"https://free.freeipapi.com/api/json/{ipAddress}"; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(url); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } } } ``` -------------------------------- ### Get IP Info with JQuery Source: https://freeipapi.com/ A simple JavaScript example using JQuery's $.get method to fetch IP address details for the current request. ```javascript const url = `https://free.freeipapi.com/api/json`; // get current request's ip info $.get(url, function (data) { console.log(data); }); ``` -------------------------------- ### Get IP Info with Rust Source: https://freeipapi.com/ An asynchronous Rust example using the 'reqwest' crate to fetch IP address details. Ensure you replace {IP-ADDRESS} with the desired IP. ```rust use reqwest; #[tokio::main] async fn main() -> Result<(), Box> { let ip_address = "{IP-ADDRESS}"; let url = format!("https://free.freeipapi.com/api/json/{}", ip_address); let response = reqwest::get(&url).await?.text().await?; println!("{}", response); Ok(()) } ``` -------------------------------- ### Get IP Info with Curl Source: https://freeipapi.com/ Examples using Curl to retrieve IP information. The first command fetches details for a specific IP, while the second retrieves the public IP of the sender. ```bash # Get the ip info curl https://free.freeipapi.com/api/json/1.1.1.1 # Get only public ip address curl https://free.freeipapi.com ``` -------------------------------- ### Get IP Geolocation Data via cURL Source: https://freeipapi.com/blog/posts/choosing-your-ip-geolocation-api Use this command-line example to quickly fetch IP geolocation data for a specific IP address using the FreeIPAPI.com API. ```bash curl https://free.freeipapi.com/api/json/8.8.8.8 ``` -------------------------------- ### Rust Project Setup: Cargo New Source: https://freeipapi.com/blog/posts/how-to-use-freeipapi-in-your-rust-project-with-caching Creates a new Rust project using Cargo. ```bash cargo new freeipapi-demo cd freeipapi-demo ``` -------------------------------- ### Get IP Info with PHP Source: https://freeipapi.com/ Use this PHP snippet to fetch IP address information by making a GET request to the API. Ensure you replace {IP-ADDRESS} with the target IP. ```php 4 1.1.1.1 -33.8688 151.209 Australia AU Canberra 61 Australia/Sydney 4000 Sydney New South Wales Oceania OC AUD en 13335 Cloudflare, Inc. false ``` -------------------------------- ### Get IP Info with Node.js (Axios) Source: https://freeipapi.com/ This Node.js snippet uses the 'axios' library to make a GET request for IP address information. Replace {IP-ADDRESS} or use the commented-out URL to get the current request's IP. ```javascript const axios = require('axios'); const ipAddress = "{IP-ADDRESS}"; const url = `https://free.freeipapi.com/api/json/${ipAddress}`; // to get specific ip's info // const url = `https://free.freeipapi.com/api/json`; // to get current request's ip info axios.get(url) .then(response => { const data = response.data; console.log(data); }) .catch(error => { console.error(error); }); ``` -------------------------------- ### JSON Payload Example Source: https://freeipapi.com/docs/api-reference/get-bulk-ip-info Construct your request body with a JSON object containing an 'ips' array of IP addresses. ```json { "ips": [ "1.1.1.1", "8.8.8.8" ] } ``` -------------------------------- ### Get All Timezones (GET) Source: https://freeipapi.com/docs/api-reference/timezones Use this endpoint to retrieve a list of all supported timezones. This API is available only for UNLIMITED plan subscribers. ```http GET /api/timezones ``` -------------------------------- ### Example JSON Response from FreeIPAPI Source: https://freeipapi.com/blog/posts/new-feature-xml-support-added This is a sample response when requesting IP information in JSON format. It includes details like IP version, address, location, and network information. ```json { "ipVersion": 4, "ipAddress": "1.1.1.1", "latitude": -33.8688, "longitude": 151.209, "countryName": "Australia", "countryCode": "AU", "capital": "Canberra", "phoneCodes": [61], "timeZones": [ "Antarctica/Macquarie", "Australia/Adelaide", "... more ..." ], "zipCode": "4000", "cityName": "Sydney", "regionName": "New South Wales", "continent": "Oceania", "continentCode": "OC", "currencies": ["AUD"], "languages": ["en"], "asn": "13335", "asnOrganization": "Cloudflare, Inc.", "isProxy": false } ``` -------------------------------- ### GET Countries API Endpoint Source: https://freeipapi.com/docs/api-reference/countries Use this GET request to retrieve a list of all available country codes. This API is part of the UNLIMITED plan. ```bash GET /api/countries ``` -------------------------------- ### Timezone API Response Example Source: https://freeipapi.com/docs/api-reference/timezone A successful response includes the country code and a list of associated timezones. For example, 'DE' returns 'Europe/Berlin' and 'Europe/Busingen'. ```json { "code": "DE", "timezones": [ "Europe/Berlin", "Europe/Busingen" ] } ``` -------------------------------- ### Get IP Info (XML) Source: https://freeipapi.com/docs/api-reference/get-ip-info This snippet shows how to retrieve IP information in XML format. You can specify an IP address in the path or omit it to get information about the requester's IP. ```APIDOC ## Get IP Info (XML) ### Description This endpoint returns the IP info of the given IP address in XML format. If no IP is provided, it defaults to the requester's IP address. ### Method GET ### Endpoint /api/xml/{ip} ### Parameters #### Path Parameters - **ip** (string) - Optional - The IP address you want to get the info of. Defaults to the requester's IP address. ### Response #### Success Response (200) - **ipVersion** (integer) - The version of the IP address (e.g., 4 or 6). - **ipAddress** (string) - The IP address. - **latitude** (number) - The latitude coordinate of the IP address location. - **longitude** (number) - The longitude coordinate of the IP address location. - **countryName** (string) - The name of the country. - **countryCode** (string) - The ISO 3166-1 alpha-2 country code. - **capital** (string) - The capital city of the country. - **phoneCodes** (integer) - The international dialing code for the country. - **timeZones** (array of strings) - Time zones associated with the IP address location. - **zipCode** (string) - The postal or zip code of the IP address location. - **cityName** (string) - The name of the city. - **regionName** (string) - The name of the region or state. - **regionCode** (string) - The code for the region or state. - **continent** (string) - The name of the continent. - **continentCode** (string) - The code for the continent. - **currencies** (string) - The currency code used in the country. - **languages** (string) - The language code spoken in the country. - **asn** (string) - The Autonomous System Number. - **asnOrganization** (string) - The organization name associated with the ASN. - **isProxy** (boolean) - Indicates if the IP address is associated with a proxy. #### Response Example ```xml 4 1.1.1.1 -33.8688 151.209 Australia AU Canberra 61 Antarctica/Macquarie Australia/Adelaide Australia/Brisbane Australia/Broken_Hill Australia/Darwin Australia/Eucla Australia/Hobart Australia/Lindeman Australia/Lord_Howe Australia/Melbourne Australia/Perth Australia/Sydney 4000 Sydney New South Wales NSW Oceania OC AUD en 13335 Cloudflare, Inc. false ``` ``` -------------------------------- ### Example Rust Application: Lookup Current IP Source: https://freeipapi.com/blog/posts/how-to-use-freeipapi-in-your-rust-project-with-caching A simple async main function that demonstrates how to use the cached get_ip_data function to fetch and print geolocation information for a given IP address. ```rust #[tokio::main] async fn main() { let ip = "8.8.8.8"; // Example: Google DNS match get_ip_data(ip.to_string()).await { Ok(data) => { println!("Country: {:?}", data.countryName); println!("City: {:?}", data.cityName); println!("ASN: {:?}", data.asn); } Err(err) => eprintln!("Failed to fetch IP data: {:?}", err), } } ``` -------------------------------- ### Get IP Info (JSON) Source: https://freeipapi.com/docs/api-reference/get-ip-info This snippet shows how to retrieve IP information in JSON format. You can specify an IP address in the path or omit it to get information about the requester's IP. ```APIDOC ## Get IP Info (JSON) ### Description This endpoint returns the IP info of the given IP address in JSON format. If no IP is provided, it defaults to the requester's IP address. ### Method GET ### Endpoint /api/json/{ip} ### Parameters #### Path Parameters - **ip** (string) - Optional - The IP address you want to get the info of. Defaults to the requester's IP address. ### Response #### Success Response (200) - **ipVersion** (integer) - The version of the IP address (e.g., 4 or 6). - **ipAddress** (string) - The IP address. - **latitude** (number) - The latitude coordinate of the IP address location. - **longitude** (number) - The longitude coordinate of the IP address location. - **countryName** (string) - The name of the country. - **countryCode** (string) - The ISO 3166-1 alpha-2 country code. - **capital** (string) - The capital city of the country. - **phoneCodes** (array of integers) - An array of international dialing codes for the country. - **timeZones** (array of strings) - An array of time zones associated with the IP address location. - **zipCode** (string) - The postal or zip code of the IP address location. - **cityName** (string) - The name of the city. - **regionName** (string) - The name of the region or state. - **regionCode** (string) - The code for the region or state. - **continent** (string) - The name of the continent. - **continentCode** (string) - The code for the continent. - **currencies** (array of strings) - An array of currency codes used in the country. - **languages** (array of strings) - An array of language codes spoken in the country. - **asn** (string) - The Autonomous System Number. - **asnOrganization** (string) - The organization name associated with the ASN. - **isProxy** (boolean) - Indicates if the IP address is associated with a proxy. #### Response Example ```json { "ipVersion": 4, "ipAddress": "1.1.1.1", "latitude": -33.8688, "longitude": 151.209, "countryName": "Australia", "countryCode": "AU", "capital": "Canberra", "phoneCodes": [61], "timeZones": [ "Antarctica/Macquarie", "Australia/Adelaide", "Australia/Brisbane", "Australia/Broken_Hill", "Australia/Darwin", "Australia/Eucla", "Australia/Hobart", "Australia/Lindeman", "Australia/Lord_Howe", "Australia/Melbourne", "Australia/Perth", "Australia/Sydney" ], "zipCode": "4000", "cityName": "Sydney", "regionName": "New South Wales", "regionCode": "NSW", "continent": "Oceania", "continentCode": "OC", "currencies": ["AUD"], "languages": ["en"], "asn": "13335", "asnOrganization": "Cloudflare, Inc.", "isProxy": false } ``` ``` -------------------------------- ### Get IP Info (XML) Source: https://freeipapi.com/docs/api-reference/get-ip-info Use this endpoint to retrieve IP information in XML format. The IP address is a path parameter. ```http GET /api/xml/{ip} ``` ```xml 4 1.1.1.1 -33.8688 151.209 Australia AU Canberra 61 Antarctica/Macquarie Australia/Adelaide Australia/Brisbane Australia/Broken_Hill Australia/Darwin Australia/Eucla Australia/Hobart Australia/Lindeman Australia/Lord_Howe Australia/Melbourne Australia/Perth Australia/Sydney 4000 Sydney New South Wales NSW Oceania OC AUD en 13335 Cloudflare, Inc. false ``` -------------------------------- ### Get IP Info (JSON) Source: https://freeipapi.com/docs/api-reference/get-ip-info Use this endpoint to retrieve IP information in JSON format. The IP address is a path parameter. ```http GET /api/json/{ip} ``` ```json { "ipVersion": 4, "ipAddress": "1.1.1.1", "latitude": -33.8688, "longitude": 151.209, "countryName": "Australia", "countryCode": "AU", "capital": "Canberra", "phoneCodes": [61], "timeZones": [ "Antarctica/Macquarie", "Australia/Adelaide", "Australia/Brisbane", "Australia/Broken_Hill", "Australia/Darwin", "Australia/Eucla", "Australia/Hobart", "Australia/Lindeman", "Australia/Lord_Howe", "Australia/Melbourne", "Australia/Perth", "Australia/Sydney" ], "zipCode": "4000", "cityName": "Sydney", "regionName": "New South Wales", "regionCode": "NSW", "continent": "Oceania", "continentCode": "OC", "currencies": [ "AUD" ], "languages": [ "en" ], "asn": "13335", "asnOrganization": "Cloudflare, Inc.", "isProxy": false } ``` -------------------------------- ### GET Country Info by Code Source: https://freeipapi.com/docs/api-reference/country Use this endpoint to retrieve detailed information about a specific country using its ISO 3166-1 alpha-2 code. This is a GET request. ```http GET /api/countries/{code} ``` -------------------------------- ### Get Languages by Code Source: https://freeipapi.com/docs/api-reference/languages This endpoint returns a list of countries that use a specified language code. Requires the UNLIMITED plan. ```APIDOC ## GET /api/languages/{code} ### Description This endpoint returns a list of countries that use the specified language code. ### Method GET ### Endpoint /api/languages/{code} ### Parameters #### Path Parameters - **code** (string) - Required - The language code to query (e.g., 'de'). ### Request Example ``` /api/languages/de ``` ### Response #### Success Response (200) - **code** (string) - The language code requested. - **total_countries** (integer) - The total number of countries found for the language. - **countries** (array) - A list of countries using the language. - **code** (string) - The country code. - **name** (string) - The name of the country. - **native** (string) - The native name of the country. - **capital** (string) - The capital city of the country. - **continent** (string) - The continent the country belongs to. #### Response Example ```json { "code": "de", "total_countries": 6, "countries": [ { "code": "AT", "name": "Austria", "native": "Österreich", "capital": "Vienna", "continent": "EU" }, { "code": "BE", "name": "Belgium", "native": "België", "capital": "Brussels", "continent": "EU" }, { "code": "CH", "name": "Switzerland", "native": "Schweiz", "capital": "Bern", "continent": "EU" }, { "code": "DE", "name": "Germany", "native": "Deutschland", "capital": "Berlin", "continent": "EU" }, { "code": "LI", "name": "Liechtenstein", "native": "Liechtenstein", "capital": "Vaduz", "continent": "EU" }, { "code": "LU", "name": "Luxembourg", "native": "Luxembourg", "capital": "Luxembourg", "continent": "EU" } ] } ``` ``` -------------------------------- ### Get IP Info with Ruby Source: https://freeipapi.com/ A Ruby script utilizing 'net/http' and 'json' to retrieve IP address details. Remember to replace {IP-ADDRESS} with the specific IP. ```ruby require 'net/http' require 'json' ip_address = "{IP-ADDRESS}" url = URI.parse("https://free.freeipapi.com/api/json/#{ip_address}") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Get.new(url.request_uri) response = http.request(request) data = JSON.parse(response.body) puts data ``` -------------------------------- ### Get Languages by Code Source: https://freeipapi.com/docs/api-reference/languages Use this endpoint to retrieve a list of countries that speak a specific language. The language code must be provided in the URL path. ```bash GET /api/languages/{code} ``` ```bash /api/languages/de ``` -------------------------------- ### Get Bulk IP Info (JSON) Source: https://freeipapi.com/docs/api-reference/get-bulk-ip-info This snippet shows how to use the POST /api/bulk/json endpoint to retrieve IP information in bulk using a JSON payload. A maximum of 50 IP addresses are allowed per request. ```APIDOC ## Get Bulk IP Info (JSON) ### Description This endpoint retrieves IP information in bulk for the requested IP addresses using a JSON payload. It is available only to UNLIMITED plan subscribers. ### Method POST ### Endpoint /api/bulk/json ### Request Body - **ips** (array[string]) - Required - A list of IP addresses to get information for. Maximum 50 IPs per request. ### Request Example ```json { "ips": [ "1.1.1.1", "8.8.8.8" ] } ``` ### Response #### Success Response (200) - **response** (object) - Contains the IP information for each requested IP address. #### Response Example ```json { "response": [ { "ip": "1.1.1.1", "hostname": "one.one.one.one", "city": "", "region": "", "country": "United States", "country_code": "US", "continent": "North America", "continent_code": "NA", "latitude": 33.9291, "longitude": -84.1175, "postal_code": "", "timezone": "America/New_York", "asn": "AS13335", "organization": "Cloudflare, Inc." }, { "ip": "8.8.8.8", "hostname": "dns.google", "city": "Mountain View", "region": "California", "country": "United States", "country_code": "US", "continent": "North America", "continent_code": "NA", "latitude": 37.4056, "longitude": -122.0775, "postal_code": "94043", "timezone": "America/Los_Angeles", "asn": "AS15169", "organization": "Google LLC" } ] } ``` ``` -------------------------------- ### Run Rust Application Source: https://freeipapi.com/blog/posts/how-to-use-freeipapi-in-your-rust-project-with-caching Command to compile and run the Rust project. ```bash cargo run ``` -------------------------------- ### Set Up Route for IP Lookup Source: https://freeipapi.com/blog/posts/how-to-use-freeipapi-in-your-laravel-project-with-caching Define a web route in Laravel that maps a URL to the lookup method in the IpController. ```php use App\Http\Controllers\IpController; Route::get('/ip-lookup', [IpController::class, 'lookup']); ``` -------------------------------- ### Get Timezone by Country Code Source: https://freeipapi.com/docs/api-reference/timezone Use this GET request to retrieve the timezone(s) associated with a specific country code. The country code should be provided as a path parameter. ```http GET /api/timezones/{code} ``` -------------------------------- ### Create IP Lookup Service with Caching Source: https://freeipapi.com/blog/posts/how-to-use-freeipapi-in-your-laravel-project-with-caching This service class handles fetching IP data from FreeIPAPI and caches the results for one hour using Laravel's Cache facade. ```php successful()) { return $response->json(); } return null; }); } } ``` -------------------------------- ### Get IP Information (Specific IP) Source: https://freeipapi.com/ This endpoint retrieves information for a specific IP address. The API accepts GET and POST methods. The 'ip' field can be passed as a query parameter or in the request body. If no 'ip' field is provided, the sender's IP address is used. ```APIDOC ## GET /api/json/{IP-ADDRESS} ### Description Retrieves detailed information about a specific IP address. ### Method GET ### Endpoint `https://free.freeipapi.com/api/json/{IP-ADDRESS}` ### Parameters #### Path Parameters - **IP-ADDRESS** (string) - Required - The IP address for which to retrieve information. ### Request Example ``` https://free.freeipapi.com/api/json/1.1.1.1 ``` ``` ```APIDOC ## GET /api/json ### Description Retrieves information for the sender's IP address. This can also be used to get public IP address. ### Method GET ### Endpoint `https://free.freeipapi.com/api/json` ### Request Example ``` https://free.freeipapi.com/api/json ``` ``` -------------------------------- ### Implement Cached IP Lookup Service in Rust Source: https://freeipapi.com/blog/posts/how-to-use-freeipapi-in-your-rust-project-with-caching Implements an asynchronous function to fetch IP data from FreeIPAPI, utilizing the 'cached' crate for automatic caching. Results are stored for 1 hour and up to 10,000 entries. ```rust use cached::proc_macro::cached; use reqwest::Error; mod model; use model::IpData; // Caching function: cache up to 10,000 entries for 1 hour #[cached(size = 10000, time = 3600)] pub async fn get_ip_data(ip: String) -> Result { let url = format!("https://free.freeipapi.com/api/json/{}", ip); let response = reqwest::get(&url).await?; let data = response.json::().await?; Ok(data) } ``` -------------------------------- ### Get Countries Source: https://freeipapi.com/docs/api-reference/countries This endpoint returns a list of all available country codes. This API is only available to users on the UNLIMITED plan. ```APIDOC ## Get Countries ### Description This endpoint returns a list of all available country codes. This API is only available to users on the UNLIMITED plan. ### Method GET ### Endpoint /api/countries ### Response #### Success Response (200) - **countries** (array) - A list of country codes. ### Response Example ```json [ "AD", "AE", "AF", ... ] ``` ``` -------------------------------- ### Get Timezone by Country Code Source: https://freeipapi.com/docs/api-reference/timezone This endpoint returns the timezone of the given country code. Requires the UNLIMITED plan. ```APIDOC ## GET /api/timezones/{code} ### Description Retrieves the timezone(s) for a specified country code. ### Method GET ### Endpoint /api/timezones/{code} ### Parameters #### Path Parameters - **code** (string) - Required - The country code for which to retrieve timezone information. ### Response #### Success Response (200) - **code** (string) - The country code. - **timezones** (array of strings) - A list of timezones associated with the country code. ### Response Example ```json { "code": "DE", "timezones": [ "Europe/Berlin", "Europe/Busingen" ] } ``` ``` -------------------------------- ### Get Bulk IP Info (XML) Source: https://freeipapi.com/docs/api-reference/get-bulk-ip-info This snippet shows how to use the POST /api/bulk/xml endpoint to retrieve IP information in bulk using an XML payload. A maximum of 50 IP addresses are allowed per request. ```APIDOC ## Get Bulk IP Info (XML) ### Description This endpoint retrieves IP information in bulk for the requested IP addresses using an XML payload. It is available only to UNLIMITED plan subscribers. ### Method POST ### Endpoint /api/bulk/xml ### Request Body - **ips** (array[string]) - Required - A list of IP addresses to get information for. Maximum 50 IPs per request. ### Request Example ```xml 1.1.1.1 8.8.8.8 ``` ### Response #### Success Response (200) - **response** (object) - Contains the IP information for each requested IP address. #### Response Example ```xml 1.1.1.1 one.one.one.one United States US North America NA 33.9291 -84.1175 America/New_York AS13335 Cloudflare, Inc. 8.8.8.8 dns.google Mountain View California United States US North America NA 37.4056 -122.0775 94043 America/Los_Angeles AS15169 Google LLC ``` ``` -------------------------------- ### Countries API Response Example Source: https://freeipapi.com/docs/api-reference/countries A successful response from the Countries API will return a JSON array containing country codes. ```json [ "AD", "AE", "AF", ... ] ``` -------------------------------- ### Use IP Lookup Service in Controller Source: https://freeipapi.com/blog/posts/how-to-use-freeipapi-in-your-laravel-project-with-caching Inject the IpLookupService into a controller to retrieve and return the user's geolocation data, leveraging the caching mechanism. ```php ipService = $ipService; } public function lookup(Request $request) { $ip = $request->ip(); // Get the user’s IP $ipData = $this->ipService->getIpData($ip); return response()->json($ipData); } } ```