SQL Server
SQL Server
Geolocation of IP Address
See more REST Examples
To get information about an IP address, there are various public web services that can be queried. I would also guess that paid-for services exist. In any case, it's likely the service is accessible via a REST API. This example demonstrates a few public geolocation API's, each of which may have limitations on the number of queries per hour/day/etc.Also, please note that this example was created on 23-Sep-2016. As time goes by, the public services referenced by this example may have changed or disappeared entirely. Make sure to do your research before assuming this example will work. The intent of this example is to give a flavor of what might be possible.
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.
-- The IP address used in this example is 104.40.211.35
DECLARE @ipAddress nvarchar(4000)
SELECT @ipAddress = '104.40.211.35'
-- First we'll try the service at freegeoip.net.
-- They have a limit of 10,000 queries per hour, and also
-- provide free source code to run your own server.
DECLARE @rest int
EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Connect to freegeoip.net
DECLARE @bTls int
SELECT @bTls = 0
DECLARE @port int
SELECT @port = 80
DECLARE @bAutoReconnect int
SELECT @bAutoReconnect = 1
EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'freegeoip.net', @port, @bTls, @bAutoReconnect
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rest
RETURN
END
-- Query the IP address to return JSON.
DECLARE @responseJson nvarchar(4000)
EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseJson OUT, 'GET', '/json/104.40.211.35'
EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rest
RETURN
END
-- Just in case we are still connected..
DECLARE @maxWaitMs int
SELECT @maxWaitMs = 10
EXEC sp_OAMethod @rest, 'Disconnect', @success OUT, @maxWaitMs
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'Load', @success OUT, @responseJson
EXEC sp_OASetProperty @json, 'EmitCompact', 0
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
PRINT @sTmp0
-- The JSON we get back looks like this:
-- {
-- "ip": "104.40.211.35",
-- "country_code": "US",
-- "country_name": "United States",
-- "region_code": "WA",
-- "region_name": "Washington",
-- "city": "Redmond",
-- "zip_code": "98052",
-- "time_zone": "America/Los_Angeles",
-- "latitude": 47.6801,
-- "longitude": -122.1206,
-- "metro_code": 819
-- }
-- Examine a few bits of information:
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'country_name'
PRINT 'country name = ' + @sTmp0
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'country_code'
PRINT 'country code = ' + @sTmp0
-- -----------------------------------------------------
-- Now to use ip-api.com, which is mostly the same..
EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'ip-api.com', @port, @bTls, @bAutoReconnect
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @json
RETURN
END
-- Query the IP address to return JSON.
EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseJson OUT, 'GET', '/json/104.40.211.35'
EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @json
RETURN
END
-- Just in case we are still connected..
EXEC sp_OAMethod @rest, 'Disconnect', @success OUT, @maxWaitMs
EXEC sp_OAMethod @json, 'Load', @success OUT, @responseJson
EXEC sp_OASetProperty @json, 'EmitCompact', 0
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
PRINT @sTmp0
-- The JSON we get back looks like this:
-- This is very strange, because the two services don't agree.
-- {
-- "as": "AS8075 Microsoft Corporation",
-- "city": "Amsterdam",
-- "country": "Netherlands",
-- "countryCode": "NL",
-- "isp": "Microsoft Corporation",
-- "lat": 52.35,
-- "lon": 4.9167,
-- "org": "Microsoft Azure",
-- "query": "104.40.211.35",
-- "region": "NH",
-- "regionName": "North Holland",
-- "status": "success",
-- "timezone": "Europe/Amsterdam",
-- "zip": "1091"
-- }
-- Examine a few bits of information:
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'country'
PRINT 'country name = ' + @sTmp0
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'countryCode'
PRINT 'country code = ' + @sTmp0
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @json
END
GO