Php 地理位置显示不同的城市不是很准确

Php 地理位置显示不同的城市不是很准确,php,geolocation,ip-geolocation,Php,Geolocation,Ip Geolocation,我使用地理定位来检测当前的城市,它没有显示确切的城市,例如我在班加罗尔,它显示的是钦奈,有时是蒂鲁瓦南普兰。请让我知道如何准确地显示当前城市。 下面是我的代码- function fetch($host) { if ( function_exists('curl_init') ) { //use cURL to fetch data $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ho

我使用地理定位来检测当前的城市,它没有显示确切的城市,例如我在班加罗尔,它显示的是钦奈,有时是蒂鲁瓦南普兰。请让我知道如何准确地显示当前城市。 下面是我的代码-

function fetch($host) {

    if ( function_exists('curl_init') ) {

        //use cURL to fetch data
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $host);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.1');
        $response = curl_exec($ch);
        curl_close ($ch);

    } else if ( ini_get('allow_url_fopen') ) {

        //fall back to fopen()
        $response = file_get_contents($host, 'r');

    } else {

        trigger_error ('geoPlugin class Error: Cannot retrieve data. Either compile PHP with cURL support or enable allow_url_fopen in php.ini ', E_USER_ERROR);
        return;

    }

    return $response;
}

function convert($amount, $float=2, $symbol=true) {

    //easily convert amounts to geolocated currency.
    if ( !is_numeric($this->currencyConverter) || $this->currencyConverter == 0 ) {
        trigger_error('geoPlugin class Notice: currencyConverter has no value.', E_USER_NOTICE);
        return $amount;
    }
    if ( !is_numeric($amount) ) {
        trigger_error ('geoPlugin class Warning: The amount passed to geoPlugin::convert is not numeric.', E_USER_WARNING);
        return $amount;
    }
    if ( $symbol === true ) {
        return $this->currencySymbol . round( ($amount * $this->currencyConverter), $float );
    } else {
        return round( ($amount * $this->currencyConverter), $float );
    }
}

问题不在于代码,而在于信息源。IP地理定位不是100%准确,你需要接受它。最好的补救办法是使用一种服务,例如(免责声明:我运行该服务),它将来自多个来源的数据关联起来,以减少误报并减轻您的体验

根据您的代码,您似乎在猜测用户的货币。在这种情况下,可以对以下端点执行HTTP get:


查看响应字段
currency

问题不在于代码,而在于信息来源。IP地理定位不是100%准确,你需要接受它。最好的补救办法是使用一种服务,例如(免责声明:我运行该服务),它将来自多个来源的数据关联起来,以减少误报并减轻您的体验

根据您的代码,您似乎在猜测用户的货币。在这种情况下,可以对以下端点执行HTTP get:


然后查看响应字段
货币

您能显示您当前的代码吗?如果你告诉我们你在做什么,你会更容易找到这本书。请使用按钮将它添加到问题中。使用正确的格式会更具可读性。请将代码附加到问题中,而不是作为注释。可以应用代码格式以提高可读性。当然,添加了代码。请建议修理。提前感谢您能出示您当前的代码吗?如果你告诉我们你在做什么,你会更容易找到这本书。请使用按钮将它添加到问题中。使用正确的格式会更具可读性。请将代码附加到问题中,而不是作为注释。可以应用代码格式以提高可读性。当然,添加了代码。请建议修理。提前谢谢