• Fabrik4.5.1 for Joomla5.2.4

    Fabrik4.5.1 is out. This update is needed for J!5.2.4

    See Announcements

Solved Get country by IP using API

dimoss

Well-Known Member
Hi all,

Searching older posts I found the following thread which seems to be an approach to get the country using an API having the user IP.
https://fabrikar.com/forums/index.php?threads/get-country.49532/#post-259049

I applied it creating a form using a calc element with the following code using the free API from https://ip-api.com/

PHP:
$ip = '{fab_ratings___ip}';
$api_url = 'http://ip-api.com/json/'.urlencode($ip);
//var_dump($api_url);exit;
$hostinfo = file_get_contents($api_url);
//var_dump($hostinfo);exit;
$hostinfo = json_decode($hostinfo);
return $hostinfo->country;

Now the funny part comes. As long as i remove the comment from 'var_dump($hostinfo)' the script gives exits with the correct info and the correct country which in my case is Greece. If I comment 'var_dump($hostinfo)' the script records a wrong country which is always Finland regardless if I use or not use a VPN! Same it happens for others which i gave the same form URL to try.

In all cases the $api_url is correct.

I have no clue why this is happening.
 
Well. I don't know why but the issue was in
PHP:
$ip = '{fab_ratings___ip}';

Strangely it works only if I append '_raw' so the following seems the solution.
PHP:
$ip = '{fab_ratings___ip_raw}';
 
Back
Top