從訪問者的 IP 獲取訪問者所在的國家/地區 - Getting visitors country from their IP

問題:

I want to get visitors country via their IP... Right now I'm using this ( http://api.hostip.info/country.php?ip= ...... )我想通過他們的 IP 獲取訪問者的國家...現在我正在使用這個( http://api.hostip.info/country.php?ip= ......)

Here is my code:這是我的代碼:

<?php

if (isset($_SERVER['HTTP_CLIENT_IP']))
{
    $real_ip_adress = $_SERVER['HTTP_CLIENT_IP'];
}

if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
    $real_ip_adress = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
    $real_ip_adress = $_SERVER['REMOTE_ADDR'];
}

$cip = $real_ip_adress;
$iptolocation = 'http://api.hostip.info/country.php?ip=' . $cip;
$creatorlocation = file_get_contents($iptolocation);

?>

Well, it's working properly, but the thing is, this returns the country code like US or CA., and not the whole country name like United States or Canada.嗯,它工作正常,但問題是,這會返回像美國或加拿大這樣的國家/地區代碼,而不是像美國或加拿大這樣的整個國家/地區名稱。

So, is there any good alternative to hostip.info offers this?那麼,hostip.info 有什麼好的替代方案嗎?

I know that I can just write some code that will eventually turn this two letters to whole country name, but I'm just too lazy to write a code that contains all countries...我知道我可以編寫一些代碼,最終將這兩個字母轉換爲整個國家/地區的名稱,但我懶得編寫包含所有國家/地區的代碼......

PS: For some reason I don't want to use any ready made CSV file or any code that will grab this information for me, something like ip2country ready made code and CSV. PS:出於某種原因,我不想使用任何現成的 CSV 文件或任何可以爲我獲取此信息的代碼,例如 ip2country 的現成代碼和 CSV。


解決方案:

參考一: https://stackoom.com/question/qfeK
參考二: Getting visitors country from their IP
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章