Links

Code Samples

To use the API easily, we wrote some codes in multiple programming languages to send a request to our API.

PHP

<?php
​
$curl = curl_init();
​
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://gregeoip.com/GeoIP?key=YOUR_API_KEY',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET'
));
​
$response = curl_exec($curl);
curl_close($curl);
​
// Output
echo $response;

Javascript (jQuery)

var settings = {
"url": "https://gregeoip.com/GeoIP?params=timezone,currency,security&key=your-api-key-goes-here",
"method": "GET",
"timeout": 0,
};
​
$.ajax(settings).done(function (response) {
console.log(response);
});

Javascript (XHR)

// WARNING: For GET requests, body is set to null by browsers.
​
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
​
xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
console.log(this.responseText);
}
});
​
xhr.open("GET", "https://gregeoip.com/GeoIP?params=timezone,currency,security&key=your-api-key-goes-here");
​
xhr.send();

Go (Native)

package main
​
import (
"fmt"
"net/http"
"io/ioutil"
)
​
func main() {
​
url := "https://gregeoip.com/GeoIP?params=timezone,currency,security&key=your-api-key-goes-here"
method := "GET"
​
client := &http.Client {
}
req, err := http.NewRequest(method, url, nil)
​
if err != nil {
fmt.Println(err)
return
}
​
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
​
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

Java (OkHttp)

OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
.url("https://gregeoip.com/GeoIP?params=timezone,currency,security&key=your-api-key-goes-here")
.method("GET", body)
.build();
Response response = client.newCall(request).execute();

VB.NET

Add this to the top of your project:
Imports System.Net
Imports System.IO
Imports System.Linq
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
This is the code:
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Try
request = DirectCast(WebRequest.Create("https://gregeoip.com/GeoIP?key=YOUR_API_KEY"), HttpWebRequest)
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Dim rawresp As String
rawresp = reader.ReadToEnd()
Dim jResults As JObject = JObject.Parse(rawresp)
Dim results As List(Of JToken) = jResults.Children().ToList()
For Each item As JProperty In results
item.CreateReader()
msgbox(item.Value("countryName"))
Next
Catch ex As Exception
MsgBox(ex.ToString)
Finally
If Not response Is Nothing Then response.Close(
End Try

C# (HttpClient)

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "https://gregeoip.com/GeoIP?params=timezone,currency,security&key=your-api-key-goes-here");
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
​

Swift

Please follow this link to retrieve the JSON data from our IP. Don't forget to replace the URL.

Objective-C

Please follow this link to retrieve the JSON data from our IP. Don't forget to replace the URL.

Kotlin

Please follow this link to retrieve the JSON data from our IP. Don't forget to replace the URL.

Python (http.client)

import http.client
​
conn = http.client.HTTPSConnection("gregeoip.com")
payload = ''
conn.request("GET", "/GeoIP?params=timezone,currency,security&key=your-api-key-goes-here", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Perl

#!/usr/bin/perl
​
use LWP::UserAgent;
use HTTP::Request;
​
my $URL = 'https://gregeoip.com/GeoIP?key=YOUR_API_KEY';
​
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
my $header = HTTP::Request->new(GET => $URL);
my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $ua->request($request);
​
if ($response->is_success){
print "URL:$URL\nHeaders:\n";
print $response->headers_as_string;
}elsif ($response->is_error){
print "Error:$URL\n";
print $response->error_as_HTML;
}

Ruby (Net::HTTP)

require "uri"
require "net/http"
​
url = URI("https://gregeoip.com/GeoIP?params=timezone,currency,security&key=your-api-key-goes-here")
​
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
​
request = Net::HTTP::Get.new(url)
​
response = https.request(request)
puts response.read_body
​

C++

#include <iostream>
#include <string>
#include <curl/curl.h>
#include <jsoncpp/json/json.h>
​
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
​
int main(void)
{
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://gregeoip.com/GeoIP?key=YOUR_API_KEY");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
Json::Reader reader;
Json::Value obj;
reader.parse(readBuffer, obj);
std::cout << obj["countryName"].asString() << std::endl;
return 0;
}

Bash

curl --location 'https://gregeoip.com/GeoIP?params=timezone%2Ccurrency%2Csecurity&key=your-api-key-goes-here'

Shell (wget)

wget --no-check-certificate --quiet \
--method GET \
--timeout=0 \
'https://gregeoip.com/GeoIP?params=timezone,currency,security&key=your-api-key-goes-here'