Abstract's IP Geolocation API comes with libraries, code snippets, guides, and more.Overview of Abstract IP Geolocation API
Abstract IP Geolocation API is a super fast and secure API to get the location-specific information of any IP address.
Apart from the location, this API can fetch ISP details and additional information, such as currency, timezone, and connection type.
How to access IP Geolocation API?
Open an account with Abstract and choose a subscription plan for the IP Geolocation API. You can choose the free plan for now, but note that the free plan has a limit of only 20,000 requests per month.
After successful signup, login to your dashboard and navigate to the IP Geolocation API. Make a note of your private API key.
How to use the IP Geolocation API?
Let's make a simple request using Postman to the IP Geolocation API endpoint.
This example is an API request for an arbitrary visitor IP address 184.108.40.206. The response indicates that the geolocation of this IP address lies in Lagos, Nigeria. The complete API response is somewhat like this.
Most of the information in this response is self-explanatory. However, you can check the API documentation from your logged account on Abstract for a complete guide on the API response and its meaning.
Building the WhatIsMyIP Website Clone with PHP/CodeIgniter and Abstract IPGeolocation API
CodeIgniter is a robust PHP framework with a minimal footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications. It is one of the earlier frameworks and has been around since 2011.
Some of the salient features of CodeIgniter include:
- Model-View-Controller Based System
- Extremely Light Weight
- Full Featured database classes with support for several platforms.
- Query Builder Database Support
- Form and Data Validation
- Security and XSS Filtering
We will leverage CodeIgniter to build the WhatIsMyIp clone webpage, a minimal replica of the https://whatismyipaddress.com/ webpage.
To follow the steps in this tutorial, you are expected to be familiar with HTML and PHP. Additionally, awareness or working knowledge of CodeIgniter would be an advantage.
Prerequisites for Project Setup
Create a separate directory that will act as the root project directory for all the project files and dependencies.
Additionally, make sure that you have a development environment that supports PHP with
- PHP 7.3 or newer
- Recent versions of CodeIgniter4 installed. You can refer to the installation guide to ensure that the CodeIgniter library is installed correctly and tested within the root project directory.
- VS Code, or any PHP-compatible IDE
Before proceeding, ensure that your default CodeIgniter development server works so that you can see the welcome page on the browser.
Follow the steps below to modify the default welcome page PHP script and the Home controller PHP script in the CodeIgniter framework code.
Step 1: Update the HTML Webpage snipper for WhatIsMyIP clone
Open the file ‘app/Views/welcome_message.php’.
Replace the entire content of this file with the code snippet below.
This code is a simple Bootstrap-based web page that displays a panel with the IP geolocation details and the pin on the map. This webpage somewhat mimics the https://whatismyipaddress.com/ homepage, as we saw earlier.
Step 2: Update the Home Controller
Locate the file ‘app/Controllers/Home.php’.
Replace the entire code with the below code snippet.
This controller calls the Abstract IP Geolocation API to fetch the specific information displayed on the webpage. The IP address is passed from the request coming from the browser. In addition, this controller extracts the ISP's name and location information, including latitude and longitude, to be displayed on the map.
Before saving this file, ensure that the placeholder <YOUR_ABSTRACT_API_KEY> is replaced with the actual private key as per your Abstract account.
Step 3: Test the Webpage
Now, you can test the webpage.
Assuming that the PHP development server is running, you can refresh or open http://localhost:8080 and see the WhatIsMyIP clone webpage.
You can see that the webpage has been rendered in a certain structure which is in line with the original https://whatismyipaddress.com/ website. However, there is no data captured related to the IP address or its geolocation.
This problem happens because the controlled code expects the IP address to be passed from the HTTP request.
On a local development server, the IP address is 127.0.0.1. This address is a loopback user IP address, not a public IP address routable over the Internet. Therefore, it is considered an invalid IP address that cannot have associated geolocation information.
To circumvent this problem, you can host the CodeIgniter application on a cloud server, such as the AWS EC2 instance, or GCP Compute engine. In that case, the captured client IP address in PHP code will reflect the actual IP address of the computer.
Another alternative is a quick hack within the code to test the IP Geolocation information displayed on this WhatIsMyIp clone webpage.
To achieve that, replace the line
We have taken an arbitrary IP address, 220.127.116.11, but you can also choose any IP address which has the correct format and is not reserved or within a private network range.
Refresh the WhatIsMyIp clown page now to check the outcome.
As you can see, the IP address is now captured, and its location coordinates and ISP details are also displayed, thanks to the Abstract IP Geolocation API.
Try using some other IP addresses, and you should be able to get their details on this page.
Using PHP CodeIgniter, you have learned how to leverage Abstract IP Geolocation API on a simple web app. As a next step, you can beautify the current page presentation to display more information, such as timezone, flag, and connection type associated with the IP address.
Go ahead and give it a shot. We can't wait to see the incredible things you can build with the Abstract APIs.
How Can You Get IP Address Details using CodeIgniter?
In CodeIgniter, you can get the IP address of the incoming HTTP request using the getIPAddress( ) function. You can also get geolocation-related information about the IP address by calling the Abstract IP Geolocation API via the PHP curl library. This API provides additional information about the IP address, such as the location coordinates, geolocation information, time zones, and ISP details.
What information can be extracted from an IP address?
An IP address is associated with the geographical location of the ISP, which is allocated the said IP address. Therefore, the IP address contains geolocation information and other secondary information related to geolocation. With Abstract IP Geolocation API, you can retrieve the primary location information of the IP address. Additionally, you can also retrieve information about the city, region, and country, along with timezone and connection type.
What is the use of IP Geolocation?
IP geolocation refers to the geographical information associated with a public IP address. It primarily contains latitude and longitude coordinates of the IP address, along with the Internet Service Provider (ISP) details. All this information is valuable in profiling Internet users for different use cases, such as localization, targeted outreach, and law enforcement.