February 13, 2017

Programming examples

In this section, you can find examples of use in some languages:

PHP

<?php
$url = 'https://ipvigilante.com/8.8.8.8/full';
// Get the JSON
$myJson = file_get_contents($url);
// Convert the JSON to an array
$result = json_decode($myJson, true);
echo '-----------' . PHP_EOL;
echo 'Status: ' . $result['status'] . PHP_EOL;
echo '-----------' . PHP_EOL;
echo 'IP: ' . $result['data']['ipv4'] . PHP_EOL;
echo 'Hostname: ' . $result['data']['hostname'] . PHP_EOL;
echo 'Continent code: ' . $result['data']['continent_code'] . PHP_EOL;
echo 'Continent name: ' . $result['data']['continent_name'] . PHP_EOL;
echo 'Country ISO code: ' . $result['data']['country_iso_code'] . PHP_EOL;
echo 'Country name: ' . $result['data']['country_name'] . PHP_EOL;
echo 'Subdivision 1 ISO code: ' . $result['data']['subdivision_1_iso_code'] . PHP_EOL;
echo 'Subdivision 1 name: ' . $result['data']['subdivision_1_name'] . PHP_EOL;
echo 'Subdivision 2 ISO code: ' . $result['data']['subdivision_2_iso_code'] . PHP_EOL;
echo 'Subdivision 2 name: ' . $result['data']['subdivision_2_name'] . PHP_EOL;
echo 'City name: ' . $result['data']['city_name'] . PHP_EOL;
echo 'Metro code: ' . $result['data']['metro_code'] . PHP_EOL;
echo 'Time zone: ' . $result['data']['time_zone'] . PHP_EOL;
echo 'Postal code: ' . $result['data']['postal_code'] . PHP_EOL;
echo 'Latitude: ' . $result['data']['latitude'] . PHP_EOL;
echo 'Longitude: ' . $result['data']['longitude'] . PHP_EOL;
echo 'Accuracy radius: ' . $result['data']['accuracy_radius'] . PHP_EOL;

To execute the program, create a new file, save it as example.php and execute

php example.php

Python

#!/usr/bin/env python

try:
    # For Python 3.0 and later
    from urllib.request import urlopen
except ImportError:
    # Fall back to Python 2's urllib2
    from urllib2 import urlopen

import json

url = ("http://ipvigilante.com/8.8.8.8/full")
response = urlopen(url)
data = response.read().decode("utf-8")
data = json.loads(data)

print('-----------')
print("Status: " + str(data["status"]))
print('-----------')
print("IPv4: " + str(data["data"]["ipv4"]))
print("Hostname: " + str(data["data"]["hostname"]))
print("Continent code: " + str(data["data"]["continent_code"]))
print("Continent name: " + str(data["data"]["continent_name"]))
print("Country ISO code: " + str(data["data"]["country_iso_code"]))
print("Country name: " + str(data["data"]["country_name"]))
print("Subdivision 1 ISO code: " + str(data["data"]["subdivision_1_iso_code"]))
print("Subdivision 1 name: " + str(data["data"]["subdivision_1_name"]))
print("Subdivision 2 ISO code: " + str(data["data"]["subdivision_2_iso_code"]))
print("Subdivision 2 name: " + str(data["data"]["subdivision_2_name"]))
print("City name: " + str(data["data"]["city_name"]))
print("Metro code: " + str(data["data"]["metro_code"]))
print("Time zone: " + str(data["data"]["time_zone"]))
print("Postal code: " + str(data["data"]["postal_code"]))
print("Latitude: " + str(data["data"]["latitude"]))
print("Longitude: " + str(data["data"]["longitude"]))
print("Accuracy radius: " + str(data["data"]["accuracy_radius"]))

To execute the program, create a new file, save it as example.py and execute

python example.py

Node.js

'use strict';
var https = require('https');

var options = {
    host: 'ipvigilante.com',
    path: '/8.8.8.8/full',
    port : 443,
    method : 'GET',
    headers: {'User-Agent': 'request'}
};

https.get(options, function (res) {
    var json = '';
    res.on('data', function (chunk) {
        json += chunk;
    });
    res.on('end', function () {
        if (res.statusCode === 200) {
            try {
                var result = JSON.parse(json);
                console.log('-----------');
                console.log('Status: ', result.status);
                console.log('-----------');
                console.log('IPv4: ', result.data.ipv4);
                console.log('Hostname: ', result.data.hostname);
                console.log('Continent code: ', result.data.continent_code);
                console.log('Continent name: ', result.data.continent_name);
                console.log('Country ISO code: ', result.data.country_iso_code);
                console.log('Country name: ', result.data.country_name);
                console.log('Subdivision 1 ISO code: ', result.data.subdivision_1_iso_code);
                console.log('Subdivision 1 name: ', result.data.subdivision_1_name);
                console.log('Subdivision 2 ISO code: ', result.data.subdivision_2_iso_code);
                console.log('Subdivision 2 name: ', result.data.subdivision_2_name);
                console.log('City name: ', result.data.city_name);
                console.log('Metro code: ', result.data.metro_code);
                console.log('Time zone: ', result.data.time_zone);
                console.log('Postal code: ', result.data.postal_code);
                console.log('Latitude: ', result.data.latitude);
                console.log('Longitude: ', result.data.longitude);
                console.log('Accuracy radius: ', result.data.accuracy_radius);
            } catch (e) {
                console.log('Error parsing JSON!');
            }
        } else {
            console.log('Status:', res.statusCode);
        }
    });
}).on('error', function (err) {
    console.log('Error:', err);
});

To execute the program, create a new file, save it as example.js and execute

node example.js