Get Country

JackGoa

Member
Hi, is there a built-in simple way (or some simple script) I can use to record a user's country and city in a hidden element when they fill in a form?
 
Ok, so then I need to do another lookup somehow to find the country from the IP?

The target audience is Africa, not Europe, so I'm sure it won't be necessary for GDRP.
 
Depends where you store the data.

There's a free and very simple API site to get geolocation details from IP's, at http://hostip.info

So a calc element (with "Only calc on save" set to yes) with something like ...

Code:
return file_get_contents('https://api.hostip.info/country.php?ip={yourtable___your_ip_element');

... should do it.

You can also get the city, although this is of very limited use. For instance, my IP shows as being in Vallejo, CA ... about 2,000 miles away:

{"country_name":"UNITED STATES","country_code":"US","city":"Vallejo, CA","ip":"75.76.15.183"}

For that you need to get the JSON ...

Code:
$hostinfo = file_get_contents('https://api.hostip.info/get_json.php?ip={yourtable___your_ip_element');
$hostinfo = json_decode($hostinfo);
return $hostinfo->city;

If you are filling multiple fields, I'd recommend a PHP form submission script (running onBeforeProcess) rather than individual calc's calling the API multiple times, and using $formModel->updateFormData('yourtable___whatever', $whatever, true) to set each value.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top