【问题标题】:Using maxmid GeoIP2 PHP API with CodeIgniter将 maxmid GeoIP2 PHP API 与 CodeIgniter 一起使用
【发布时间】:2014-06-21 20:09:03
【问题描述】:

我正在尝试在 CodeIgniter 中使用 GeoIP2 PHP API (https://github.com/maxmind/GeoIP2-php)。如何加载 GeoIP2 并将其用于用户地理定位?

我试过这样加载它:

$this->load->library("GeoIp2/Database/Reader");

    require APPPATH . "libraries/GeoIp2/ProviderInterface.php";
    require APPPATH . "libraries/GeoIp2/Database/Reader.php";

$this->load->file("GeoIp2/ProviderInterface");
$this->load->library("GeoIp2/Database/Reader");

我收到此错误:“无法加载请求的文件:ProviderInterface”

我看过这个 Namespace in PHP CodeIgniter Framework ,但我对命名空间没有什么经验。

这个没有成功,我没有赢,我真的不知道如何在 CodeIgniter 中实现这个。

【问题讨论】:

    标签: php codeigniter geolocation codeigniter-2 maxmind


    【解决方案1】:

    我试图找到这个问题的解决方案。但是在stackoverflow上找不到。我在这里编写自己的代码。也许,它会对某人有所帮助。我在utility_helper.php 文件中添加了一个新函数:

    function get_ip_country_code($ip_address) {
    
      require APPPATH .'third_party/GeoIP2/autoload.php';
      $reader = new GeoIp2\Database\Reader(FCPATH.'public/geoip/GeoIP2-Country.mmdb');
    
      $record = $reader->country($ip_address);
    
      return $record->country->isoCode;
    }
    

    我把GeoIP2库放在third_party文件夹里,把mmdb文件放在public文件夹里。这对我来说可以。我希望它能节省别人的时间:)

    【讨论】:

      【解决方案2】:

      GeoIp2 php sdk 利用了 PHP 的命名空间功能,CodeIgniter 框架不支持,这就是您尝试加载库时收到错误的原因。您链接到的帖子提供了使用 spl_autoload 的解决方案,但是我不使用 CodeIgniter,也没有使用 GeopIp2 php sdk 对其进行测试。

      【讨论】:

        【解决方案3】:

        在 CodeIgniter 中嵌入它的方法很少。

        首先,您需要将其包含在脚本中:

        require_once( 'GeoIp2/vendor/autoload.php' );
        use GeoIp2\Database\Reader;
        

        接下来,我给Reader()打电话询问检测方法

        $reader = new Reader('GeoIp2/GeoIP2-City.mmdb');
        $record = $reader->city($ip);
        
        // Country (code)
        $record->country->isoCode; 
        
        // State
        $record->mostSpecificSubdivision->name; 
        
        // City
        $record->city->name; 
        
        // zip code
        $record->postal->code; 
        

        我刚刚在 CodeIgniter 3x 上对此进行了测试并且工作正常。

        我使用了桥梁课程。在/application/libraries 中创建一个名为CI_GeoIp2.php 的文件并添加以下代码。

        <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
        
        /**
         * GeoIp2 Class
         *
         * @package       CodeIgniter
         * @subpackage  Libraries
         * @category      GeoIp2
         * @author        Timothy Marois <timothymarois@gmail.com>
         */
        require_once( APPPATH . 'third_party/GeoIp2/vendor/autoload.php' );
        use GeoIp2\Database\Reader;
        
        class CI_GeoIp2 { 
        
            protected $record;
            protected $database_path = 'third_party/GeoIp2/GeoIP2-City.mmdb';
        
            public function __construct() {
        
                $ci =& get_instance();
                $reader = new Reader(APPPATH.$this->database_path);
        
                $ip = $ci->input->ip_address();
                if ($ci->input->valid_ip($ip)) {
                    $this->record = $reader->city($ip);
                }
        
                log_message('debug', "CI_GeoIp2 Class Initialized");
            }
        
        
            /**
             * getState()
             * @return state
             */
            public function getState() {
                return $this->record->mostSpecificSubdivision->name;;
            }
        
        
            /**
             * getState()
             * @return country code "US/CA etc"
             */
            public function getCountryCode() {
                return $this->record->country->isoCode;
            }
        
        
            /**
             * getCity()
             * @return city name
             */
            public function getCity() {
                return $this->record->city->name;
            }
        
        
            /**
             * getZipCode()
             * @return Zip Code (#)
             */
            public function getZipCode() {
                return $this->record->postal->code;
            }
        
        
            /**
             * getRawRecord()
             * (if you want to manually extract objects)
             *
             * @return object of all items
             */
            public function getRawRecord() {
                return $this->record;
            }
        
        }
        

        现在您可以使用自动加载或加载它

         $this->load->library("CI_GeoIp2");
        

        我更喜欢在 autoload.php 配置下像这样自动加载它

         $autoload['libraries'] = array('CI_GeoIp2'=>'Location');
        

        所以在我使用的脚本中,

        $this->Location->getState() 
        $this->Location->getCity() 
        

        ...等等

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-04-20
          • 2023-04-05
          • 1970-01-01
          • 1970-01-01
          • 2014-01-02
          • 1970-01-01
          • 1970-01-01
          • 2017-01-07
          相关资源
          最近更新 更多