本文介绍了如何使用PHP获取页面的访问者国家?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关网站我发展,我希望它喜欢说有多少用户在每个国家都访问我的网站,但我很好奇的是如何让用户的国家(从​​他们的IP,也许?)?我环顾四周为在小时小时API,但我找不到像样的东西。有没有人有什么建议?感谢您的帮助。)

For a website I'm developing, I want it to like say how many users from each country have visited my site, but what I'm curious is how to get the user's country (From their IP, Maybe?)? I've looked around for APIs for hours upon hours but I couldn't find anything decent. Does anyone have any recommendations? Thanks for the help :).

推荐答案

您可以使用外部API的如

You can use external API's like geoplugin.net

$xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=76.109.14.196");
echo $xml->geoplugin_countryName ;

输出国家

United States

完整的XML响应

Full XML Response

<geoPlugin>
<geoplugin_request>76.109.14.196</geoplugin_request>
<geoplugin_status>200</geoplugin_status>
<geoplugin_city>West Palm Beach</geoplugin_city>
<geoplugin_region>FL</geoplugin_region>
<geoplugin_areaCode>561</geoplugin_areaCode>
<geoplugin_dmaCode>548</geoplugin_dmaCode>
<geoplugin_countryCode>US</geoplugin_countryCode>
<geoplugin_countryName>United States</geoplugin_countryName>
<geoplugin_continentCode>NA</geoplugin_continentCode>
<geoplugin_latitude>26.761600494385</geoplugin_latitude>
<geoplugin_longitude>-80.091598510742</geoplugin_longitude>
<geoplugin_regionCode>FL</geoplugin_regionCode>
<geoplugin_regionName>Florida</geoplugin_regionName>
<geoplugin_currencyCode>USD</geoplugin_currencyCode>
<geoplugin_currencySymbol>&#36;</geoplugin_currencySymbol>
<geoplugin_currencyConverter>1</geoplugin_currencyConverter>
</geoPlugin>

简单的函数来获取IP

Simple Function to Get IP

function getIP() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (! empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

这篇关于如何使用PHP获取页面的访问者国家?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:10