Post: Geo IP Lookup w/API
10-18-2015, 07:36 PM #1
Octolus
I defeated!
(adsbygoogle = window.adsbygoogle || []).push({});
You must login or register to view this content.


This is a Geo IP Lookup using an API. In this example, we're using my completely free and accurate API. My API Returns JSON, so we will be connecting to my api through curl. Then decode using the "json_decode" function.

I've made this look a little prettier by using Bootstrap 3, for Tables.

    
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" integrity="sha256-MfvZlkHCEqatNoGiOXveE8FIwMzZg4W85qfrfIFBfYc= sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
<div class="container" style="padding-top:50px">
<?php
if(isset($_GET['ip']))
{
if (!filter_var($_GET['ip'], FILTER_VALIDATE_IP) === false)
{

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://octolus.net/api/geo?ip='.$_GET['ip'],
CURLOPT_USERAGENT => 'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14'
));
$resp = curl_exec($curl);
curl_close($curl);

$decoded = json_decode($resp);

echo '<table class="table table-bordered">';

echo '<tr><th>Hostname:</th><td>'.$decoded->{'hostname'}.'</td></tr>';
echo '<tr><th>Country:</th><td>'.$decoded->{'country_name'}.'</td></tr>';
echo '<tr><th>Country Code:</th><td>'.$decoded->{'country_code'}.'</td></tr>';
echo '<tr><th>City:</th><td>'.$decoded->{'city'}.'</td></tr>';
echo '<tr><th>Postal Code:</th><td>'.$decoded->{'postal_code'}.'</td></tr>';
echo '<tr><th>Latitude:</th><td>'.$decoded->{'latitude'}.'</td></tr>';
echo '<tr><th>Longitude:</th><td>'.$decoded->{'longitude'}.'</td></tr>';

echo '</table>';

}
}

?>
</div>


And here is what my API Returns:
    {"continent_code":"NA","country_code":"US","country_code3":"USA","country_name":"United States","region":"CA","city":"Mountain View","postal_code":"94040","latitude":37.386001586914,"longitude":-122.08380126953,"dma_code":807,"area_code":650,"ip":"8.8.8.8","hostname":"google-public-dns-a.google.com"}

Last edited by Octolus ; 01-01-2016 at 10:03 PM.

The following 6 users say thank you to Octolus for this useful post:

DoozyXGod, Kryptus, Norway-_-1999, RTE, SK33MR
01-01-2016, 04:42 PM #2
Passion
League Champion
Originally posted by Octolus View Post
You must login or register to view this content.


This is a Geo IP Lookup using an API. In this example, we're using my completely free and accurate API. My API Returns JSON, so we will be connecting to my api through curl. Then decode using the "json_decode" function.

I've made this look a little prettier by using Bootstrap 3, for Tables.

    
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" integrity="sha256-MfvZlkHCEqatNoGiOXveE8FIwMzZg4W85qfrfIFBfYc= sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
<div class="container" style="padding-top:50px">
<?php
if(isset($_GET['ip']))
{
if (!filter_var($_GET['ip'], FILTER_VALIDATE_IP) === false)
{

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://octolus.net/api/geo?ip='.$_GET['ip'],
CURLOPT_USERAGENT => 'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14'
));
$resp = curl_exec($curl);
curl_close($curl);

$decoded = json_decode($resp);

echo '<table class="table table-bordered">';

echo '<tr><th>Hostname:</th><td>'.$decoded->{'hostname'}.'</td></tr>';
echo '<tr><th>Country:</th><td>'.$decoded->{'country_name'}.'</td></tr>';
echo '<tr><th>Country Code:</th><td>'.$decoded->{'country_code'}.'</td></tr>';
echo '<tr><th>City:</th><td>'.$decoded->{'city'}.'</td></tr>';
echo '<tr><th>Postal Code:</th><td>'.$decoded->{'postal_code'}.'</td></tr>';
echo '<tr><th>Latitude:</th><td>'.$decoded->{'latitude'}.'</td></tr>';
echo '<tr><th>Longitude:</th><td>'.$decoded->{'longitude'}.'</td></tr>';

echo '</table>';

}
}

?>
</div>


And here is what my API Returns:
    {"continent_code":"NA","country_code":"US","country_code3":"USA","country_name":"United States","region":"CA","city":"Mountain View","postal_code":"94040","latitude":37.386001586914,"longitude":-122.08380126953,"dma_code":807,"area_code":650,"ip":"8.8.8.8","hostname":"google-public-dns-a.google.com"}



API doesn't work.. Them feels
01-01-2016, 08:21 PM #3
Octolus
I defeated!
Originally posted by Passion View Post
API doesn't work.. Them feels


Works for me, are you setting user-agent header?
01-01-2016, 08:23 PM #4
Passion
League Champion
Originally posted by Octolus View Post
Works for me, are you setting user-agent header?


Yes, i just get a empty response.
Could it be my web server denying it?
01-01-2016, 09:24 PM #5
Octolus
I defeated!
Originally posted by Passion View Post
Yes, i just get a empty response.
Could it be my web server denying it?


Could you specify the headers that it's returning (200 OK, 403, 500 etc). Might be issue on my end.
01-01-2016, 09:24 PM #6
Passion
League Champion
Originally posted by octolus View Post
could you specify the headers that it's returning (200 ok, 403, 500 etc). Might be issue on my end.


' null '
01-01-2016, 10:03 PM #7
Octolus
I defeated!
Originally posted by Passion View Post
' null '


Fixed. Make sure to use HTTP instead of HTTPS.

The following user thanked Octolus for this useful post:

Passion
01-01-2016, 10:04 PM #8
Passion
League Champion
Originally posted by Octolus View Post
Fixed. Make sure to use HTTP instead of HTTPS.


Thanks!
01-03-2016, 04:29 AM #9
Devious
Treasure hunter
I need to learn php Sal
04-03-2016, 06:15 PM #10
Octolus
I defeated!
Still working, also HTTPS works fine.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo