【问题标题】:How to get country code and currency code by IP-address? [closed]如何通过IP地址获取国家代码和货币代码? [关闭]
【发布时间】:2011-08-10 03:01:18
【问题描述】:

我是 zend 框架的新手。我想通过IP地址获取货币代码,国家代码。

我可以有任何示例网址吗?

请帮帮我...

提前致谢。

【问题讨论】:

    标签: php zend-framework geolocation ip-address


    【解决方案1】:

    您应该能够为此使用 MaxMind 数据库。

    http://www.maxmind.com/app/country

    【讨论】:

      【解决方案2】:

      您将需要geoip 之类的东西——我最近使用的另一个是基于订阅的(一时想不起它的名字)。

      【讨论】:

        【解决方案3】:

        也许这个也应该帮助http://api.ip2.cc.nyud.net/?api=cname&ip=112.197.167.19

        还有一个很好的问题Good php API for extracting country code from IP?也许你可以在zend框架中创建一个提取国家代码和货币代码的插件。

        【讨论】:

          【解决方案4】:

          非常感谢 jmathaiToonMarinerexperimentX 的宝贵建议。

          但我有简单的解决方案

           public function getCountryIp()
          {
              $currency = new Zend_Currency();
              $countryCode = $this->getCountryFromIP();
              $currencyCode = $currency->getCurrencyList($countryCode);
              $localCurrency = $this->currency('USD',$currencyCode[0],50);
              $var['currencyCode'] = $currencyCode[0];
              $var['currency'] = $localCurrency;
              return $var;
          }
          
          
          
          //use to convert currency
          
          
          
          public function currency($from_Currency, $to_Currency, $amount)
           {
                  $amount = urlencode($amount);
                  $from_Currency = urlencode($from_Currency);
                  $to_Currency = urlencode($to_Currency);
                  $url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
                  $ch = curl_init();
                  $timeout = 0;
                  curl_setopt($ch, CURLOPT_URL, $url);
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
                  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
                  $rawdata = curl_exec($ch);
                  curl_close($ch);
                  $data = explode('"', $rawdata);
                  $data = explode(' ', $data['3']);
                  $stripped = ereg_replace("[^A-Za-z0-9.\+]", "", $data['0']);//remove special char
                  return round($stripped,3);
          //        $var = $data['0'];
          //        return $var;
          //        return round($var, 8);
              }
          
           //get ip-address and show country code
          
          
           public function getCountryFromIP()
           {
               $ip = $_SERVER['REMOTE_ADDR'];
          
              $country = exec("whois $ip  | grep -i country"); // Run a local whois and get the result back
              //$country = strtolower($country); // Make all text lower case so we can use str_replace happily
              // Clean up the results as some whois results come back with odd results, this should cater for most issues
              $country = str_replace("country:", "", "$country");
              $country = str_replace("Country:", "", "$country");
              $country = str_replace("Country :", "", "$country");
              $country = str_replace("country :", "", "$country");
              $country = str_replace("network:country-code:", "", "$country");
              $country = str_replace("network:Country-Code:", "", "$country");
              $country = str_replace("Network:Country-Code:", "", "$country");
              $country = str_replace("network:organization-", "", "$country");
              $country = str_replace("network:organization-usa", "us", "$country");
              $country = str_replace("network:country-code;i:us", "us", "$country");
              $country = str_replace("eu#countryisreallysomewhereinafricanregion", "af", "$country");
              $country = str_replace("", "", "$country");
              $country = str_replace("countryunderunadministration", "", "$country");
              $country = str_replace(" ", "", "$country");
          
              return $country;
           }
          

          【讨论】:

          • 修改你的代码以使用这个 url ...https://www.google.com/finance/converter?a=1000&from=USD&to=AUD
          【解决方案5】:

          您可以使用我的服务,http://ipinfo.io API 来获取国家代码:

          function get_country($ip) {
              return file_get_contents("http://ipinfo.io/{$ip}/country");
          }
          
          echo get_country("8.8.8.8"); // => US
          

          如果你对其他细节感兴趣,你可以做一个更通用的函数:

          function ip_details($ip) {
              $json = file_get_contents("http://ipinfo.io/{$ip}");
              $details = json_decode($json);
              return $details;
          }
          
          $details = ip_details("8.8.8.8");
          
          echo $details->city;     // => Mountain View
          echo $details->country;  // => US
          echo $details->org;      // => AS15169 Google Inc.
          echo $details->hostname; // => google-public-dns-a.google.com
          

          我在这些示例中使用了 IP 8.8.8.8,但如果您想了解用户 IP 的详细信息,只需传入 $_SERVER['REMOTE_ADDR']。更多详情请访问http://ipinfo.io/developers

          您可以从http://country.io/data/ 获取国家/地区代码到货币代码的映射,并将其添加到您的代码中。这是一个简单的例子:

          function getCurrenyCode($country_code) {
              $currency_codes = array(
                  'GB' => 'GBP',
                  'FR' => 'EUR',
                  'DE' => 'EUR',
                  'IT' => 'EUR',
              );
          
              if(isset($currency_codes[$country_code])) {
                  return $curreny_codes[$country_code];
              }
          
              return 'USD'; // Default to USD
          }
          

          【讨论】:

            【解决方案6】:
            (new Zend_Currency(null, 'GB'))->getShortName();
            

            返回string 'GBP'

            【讨论】:

              【解决方案7】:

              http://www.geoplugin.net/json.gp?ip="ip address here"获取详细的国家代码、货币、货币转换器、货币符号等

              【讨论】:

              • 这不是一个好的解决方案,API 是使用 HTTP 而不是 HTTPS 公开的。
              【解决方案8】:

              您可以轻松地使用https://ip-api.io 完成此任务。

              【讨论】:

              • 如果有一个调用此 API 的示例会很有用。
              • 它不是免费的。他们没有任何免费使用的规则。
              【解决方案9】:

              一个基于 ipdata.co 的示例,它直接从 IP 地址为您提供货币符号和代码。

              此答案使用非常有限的“测试”API 密钥,仅用于测试几个调用。 Signup 获取您自己的免费 API 密钥,每天最多可收到 1500 个开发请求。

              该 API 还具有 10 个全局端点,每个端点能够每秒处理超过 10,000 次调用!

              $ip = '78.8.53.5';
              $details = json_decode(file_get_contents("https://api.ipdata.co/{$ip}?api-key=test"));
              echo $details->country_name;
              //Poland
              echo $details->city;
              //Głogów
              echo $details->currency;
              // PLN
              echo $details->currency_symbol;
              // zł
              

              免责声明

              我创建了这个服务。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2023-04-04
                • 1970-01-01
                • 2015-03-07
                • 2013-07-02
                • 2014-10-21
                • 1970-01-01
                相关资源
                最近更新 更多