SQL Server
SQL Server
ipapi.co IPv4 Geolocation Lookup
See more Geolocation Examples
Demonstrates how to lookup Geolocation data for an IPv4 address using the ipapi.co REST API.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Lookup an IPv4 address: 149.250.207.170 (this was a randomly chosen address)
DECLARE @jsonStr nvarchar(4000)
EXEC sp_OAMethod @http, 'QuickGetStr', @jsonStr OUT, 'https://ipapi.co/149.250.207.170/json'
EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
RETURN
END
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OASetProperty @json, 'EmitCompact', 0
EXEC sp_OAMethod @json, 'Load', @success OUT, @jsonStr
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
PRINT @sTmp0
-- Sample output:
-- Use this online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
-- {
-- "ip": "149.250.207.170",
-- "city": "B�blingen",
-- "region": "Baden-W�rttemberg",
-- "region_code": null,
-- "country": "DE",
-- "country_name": "Germany",
-- "continent_code": "EU",
-- "in_eu": true,
-- "postal": null,
-- "latitude": null,
-- "longitude": null,
-- "timezone": null,
-- "utc_offset": null,
-- "country_calling_code": "+49",
-- "currency": "EUR",
-- "languages": "de",
-- "asn": "AS15854",
-- "org": "EntServ Deutschland GmbH"
-- }
DECLARE @ip nvarchar(4000)
DECLARE @city nvarchar(4000)
DECLARE @region nvarchar(4000)
DECLARE @region_code nvarchar(4000)
DECLARE @country nvarchar(4000)
DECLARE @country_name nvarchar(4000)
DECLARE @continent_code nvarchar(4000)
DECLARE @in_eu int
DECLARE @postal nvarchar(4000)
DECLARE @latitude nvarchar(4000)
DECLARE @longitude nvarchar(4000)
DECLARE @timezone nvarchar(4000)
DECLARE @utc_offset nvarchar(4000)
DECLARE @country_calling_code nvarchar(4000)
DECLARE @currency nvarchar(4000)
DECLARE @languages nvarchar(4000)
DECLARE @asn nvarchar(4000)
DECLARE @org nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @ip OUT, 'ip'
EXEC sp_OAMethod @json, 'StringOf', @city OUT, 'city'
EXEC sp_OAMethod @json, 'StringOf', @region OUT, 'region'
EXEC sp_OAMethod @json, 'StringOf', @region_code OUT, 'region_code'
EXEC sp_OAMethod @json, 'StringOf', @country OUT, 'country'
EXEC sp_OAMethod @json, 'StringOf', @country_name OUT, 'country_name'
EXEC sp_OAMethod @json, 'StringOf', @continent_code OUT, 'continent_code'
EXEC sp_OAMethod @json, 'BoolOf', @in_eu OUT, 'in_eu'
EXEC sp_OAMethod @json, 'StringOf', @postal OUT, 'postal'
EXEC sp_OAMethod @json, 'StringOf', @latitude OUT, 'latitude'
EXEC sp_OAMethod @json, 'StringOf', @longitude OUT, 'longitude'
EXEC sp_OAMethod @json, 'StringOf', @timezone OUT, 'timezone'
EXEC sp_OAMethod @json, 'StringOf', @utc_offset OUT, 'utc_offset'
EXEC sp_OAMethod @json, 'StringOf', @country_calling_code OUT, 'country_calling_code'
EXEC sp_OAMethod @json, 'StringOf', @currency OUT, 'currency'
EXEC sp_OAMethod @json, 'StringOf', @languages OUT, 'languages'
EXEC sp_OAMethod @json, 'StringOf', @asn OUT, 'asn'
EXEC sp_OAMethod @json, 'StringOf', @org OUT, 'org'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
END
GO