Bayesean Blog - Desktop, Mobile and IOT Developer Blog


Create a Visual IP Address Geolocation with PHP

Posted on 21st May 2017 in Bootstrap 4, CSS


geolocate.png

Creating a PHP PDO IP Address Geolocation Map

Background

This is an update of the original Blog that used Freegeoip.net API a then free API.  As of July 2018 the old Freegeoip API was discontinued and a completely re-designed API has been provided at http://api.ipstack.com. While the new API offers the same capabilities as the old one and also has the option of returning data in the legacy format, the API URL has now changed and all users are required to sign up for a free API Access Key to use the service. Fortunately the limit is 10000 calls per month. I have updated the code in the Github repository and I removed the Direct Viewer.

The purpose of the project is to update ip-addresses on to a World Map as shown in action using my own personal signed up API key.

geolocationphp.gif

Uses

  • PHP server side language use PHP 5.6 and higher.
  • JVector Maps. – Javascript based library .
    • jVectorMap uses only native browser technologies like JavaScript, CSS, HTML, SVG or VML. No Flash or any other proprietary browser plug-in is required. This allows jVectorMap to work in all modern mobile browsers.
  • Updated to Bootstrap 4 (framework).
  • Geolocation Library – https://ipstack.com.
    • Head over to https://ipstack.com and follow the instructions to create your account and obtain your access token.
    • If you only need basic IP to Geolocation data and do not require more than 10,000 requests per month, you can use the free account. If you'd like more advanced features or more requests than included in the free account you will need to choose one of the paid options.
    • You can find an overview of all available plans at https://ipstack.com/productJSON – (JavaScript Object Notation) is a lightweight data-interchange format. Designed to easily read and write. Quick to parse and generate.
    • While the old freegeoip API was limited to provide only the most basic IP to location data, our new API provides more than 20 additional data points including Language, Time Zone, Current Time, Currencies, Connection & ASN Information, and much more. To learn more about all the data points available
    • 10000 free calls per month.

Creating an IPStack account

 

Creating a free account is effortless and on signing up you are presented with a key and a lot of information on how to access the IPStack site. I have updated the SRC code and simply add your key in to the code where asked for and the project will run normally.

NewAPIConnect22.png

Geolocation Json Received Information

Calling the newer https://ipstack.com Server…

The old to the new increased available results...

Changed from

{"ip":"12.67.89.23","country_code":"US","country_name":"United

States","region_code":"","region_name":"","city":"","zip_code":"","time_zone":"","latitude":37.751,"longitude":-9

7.822,"metro_code":0}

Updated to

{"ip":"101.23.178.100","type":"ipv4","continent_code":"AS","continent_name":"Asia","country_code":"CN","

country_name":"China","region_code":"HE","region_name":"Hebei","city":"Hebei","zip":null,"latitude":39.8897,

"longitude":115.275,"location":{"geoname_id":1808775,"capital":"Beijing","languages":

[{"code":"zh","name":"Chinese","native":"\u4e2d\u6587"}],"country_flag":"http:\/\/assets.ipstack.com\/flags\/cn.

svg","country_flag_emoji":"\ud83c\udde8\ud83c\uddf3","country_flag_emoji_unicode":"U+1F1E8

U+1F1F3","calling_code":"86","is_eu":false}}

 

Coding the Project

Geolocate.php (Updated)

This holds server side code that receives the IP Address in a ‘Post Request’ then calls the https://ipstack.com API rest-server and receives the JSON response.

The JSON is decoded to an Array and this then adjusted to fit the Jvector Map marker format. This JSON styled object is sent to the Browser.

if (!empty($_POST)){
   $ip =  trim($_POST['ip']);
   if (!filter_var($ip, FILTER_VALIDATE_IP)){
     $ip = " Is not a valid ip address";
      }
      Else
{         //Set the path
$ipaddress ="http://api.ipstack.com/".trim($_POST['ip'])."?access_key=yourfreeAPIkey";

//Reads the file at the path returns a string (no options used)

$ipaddress = file_get_contents($ipaddress);
//  Alert ip info
$info = $ipaddress;

// decode the jSON string received into an associative array in anPHP variable

$response = json_decode($ipaddress,true);

if (empty($response)){
$phpstr = "NO IP address sent by Host";
}else{}
// Manipulate the array into a map marker JSON Object format
$new = "[{'latLng'  : [" .$response['latitude']. ", ".$response['longitude']. "] ,  'name':  '".$response["country_name"]."'}]";
}

//Send to  Javascript (client) as a JSON type Object which in PHP means an Array (No direct translation between PHP objects and JSON objects)
$phpstr = $new;
}
Adding the code for the JvectorMap - Javascript receives the PHP variable into a Javascript variable object which is then directly used by JvectorMap.

 

LICENCE MIT

Feel free to download the Code from The Github Repository,

 

Download Code

 

Happy Coding


Lauro      Commented   3 months ago Reply

Congratulations on the Code! This example can be used in DBGrid to make the Tree feature. | Customers A | -Sales $ 100.00

Add a Comment

9+5

Recent News

Delphi A Professional VCL DBGrid Part Four
Delphi A Professional VCL DBGrid Part Three
Delphi A Professional VCL DBGrid Part Two
Delphi A Professional VCL DBGrid Part One
Delphi VCL Buttons in DBGrid
Two Helper Apps for Delphi LibUSB
Delphi Libusb Library Introduction
Delphi Object directly to a Json string in a REST Client

Categories

Bootstrap 4
Delphi VCL
Delphi FMX
Ajax
Bootstrap 3
CSS
XE4>Delphi > XE4
Delphi < XE4
PHP

Archives

June 2019

Delphi A Professional VCL DBGrid Part Four

May 2019

Delphi A Professional VCL DBGrid Part Three

April 2019

Delphi A Professional VCL DBGrid Part Two

March 2019

Delphi A Professional VCL DBGrid Part One

November 2018

Delphi VCL Buttons in DBGrid

October 2018

Two Helper Apps for Delphi LibUSB

September 2018

Delphi Libusb Library Introduction

August 2018

Delphi Object directly to a Json string in a REST Client
Delphi using Environment Variables in your App

July 2018

Delphi FMX Leaflet Plotter using OSM Maps

June 2018

C2PAS32 Convertor Application
C to Delphi Open Source Convertors Shootout
Delphi command-line programs with DOSCommand

May 2018

Delphi PDF Embedded viewer with PDF.js

March 2018

Delphi FMX - Changing TCharacter to TCharHelper
Make Your Delphi App POP using Javascript!

January 2018

Delphi FMX Dashboard using Chart.JS
Delphi FMX Form Docking

December 2017

PHP Slim REST Server & Delphi Auth Part 5

November 2017

Delphi FMX REST Client App Part 4

October 2017

Delphi VCL REST Pricing Client App Part 3

September 2017

Delphi REST VCL Client Basic Auth Part 2B

August 2017

Delphi REST Client Part 2A
PHP PDO REST Server Part 1

July 2017

PHP REST Server and Delphi Client Intro

June 2017

Delphi SQLite Encryptor-Decryptor Tool
Updating Applications Manifest using Delphi

May 2017

Create a Visual IP Address Geolocation with PHP

March 2017

PHP Downloader using Countdown timer
PHP File Downloader from a Inbox Selection

February 2017

Javascript Image-File Uploader with ThumbViewer

January 2017

Morris Charts and PHP-PDO

December 2016

CSS to create a functional Toggle Button