To determine the IAB category for an IP address using IP2Location.io in Erlang, you can use the IP2Location.io API to retrieve geolocation and related data. For more info, please see the IAB categories.
sudo apt install erlang-jiffy
-module(test).
-export([runtest/0]).
runtest() ->
Key = "YOUR_API_KEY",
IP = "8.8.8.8",
ssl:start(),
inets:start(),
MyParams = uri_string:compose_query([{"format", "json"}, {"key", Key}, {"ip", IP}]),
case httpc:request(get, {"https://api.ip2location.io/?" ++ MyParams, []}, [{ssl, [{versions, ['tlsv1.2']}]}, {autoredirect, false}], []) of
{ok, {{_, 200, _}, _, Body}} ->
Result = jiffy:decode(unicode:characters_to_binary(Body,unicode,utf8),[return_maps]),
case maps:is_key(<<"ads_category">>, Result) of
true ->
IAB = maps:get(<<"ads_category">>, Result),
io:format("The IAB category for IP ~s is ~s.~n", [IP, IAB]);
_ ->
io:format("ERROR: The IAB category field requires a paid subscription to the Security plan.~n", [])
end;
{ok, {{_, 400, _}, _, Body}} ->
Result = jiffy:decode(unicode:characters_to_binary(Body,unicode,utf8),[return_maps]),
case maps:is_key(<<"error">>, Result) of
true ->
Error = maps:get(<<"error">>, Result),
io:format("ERROR: ~s~n", [maps:get(<<"error_message">>, Error)]);
_ ->
io:format("ERROR: ~p~n", [Result])
end;
{ok, {{_, 401, _}, _, Body}} ->
Result = jiffy:decode(unicode:characters_to_binary(Body,unicode,utf8),[return_maps]),
case maps:is_key(<<"error">>, Result) of
true ->
Error = maps:get(<<"error">>, Result),
io:format("ERROR: ~s~n", [maps:get(<<"error_message">>, Error)]);
_ ->
io:format("ERROR: ~p~n", [Result])
end;
{error, Reason} ->
io:format("ERROR ~p~n", [Reason])
end.
erl
c(test).
test:runtest().
This script will output the IAB category of specified IP address. Make sure to replace 8.8.8.8 with the IP address you want and replace YOUR_API_KEY to your own API key.
Empower your applications with accurate IP geolocation information now.
Try It for Free