dongxiji0687 2019-06-08 16:43
浏览 115

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

I'm using geolocation to detect current city which is not showing the exact city, for example i am in Bangalore and it shows Chennai or sometimes Thiruvanantpuram. Please let me know how to make it accurate to show current city. Below is my code -

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 );
    }
}
  • 写回答

1条回答 默认 最新

  • douguai7291 2019-06-27 22:54
    关注

    The issue is not with your code but the source of information. IP geolocation is not 100% accurate and you need to live with it. The best remedy is to use a service such as Ipregistry (disclaimer: I run the service) that correlates data from multiple sources in order to reduce false positives and mitigate what you experience.

    Based on your code, it looks like you are guessing your users' currency. In that case, you could perform an HTTP get to the following endpoint:

    https://api.ipregistry.co/?key=tryout

    and look at the response field currency.

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?