codeigniter 代码点火器|未找到类“GeoIp2\Database\Reader”

8tntrjer  于 2023-03-06  发布在  其他
关注(0)|答案(1)|浏览(133)

我想听听下面这句话:
https://stackoverflow.com/a/34185462/20508390
首先我通过composer安装这个库:

https://github.com/maxmind/GeoIP2-php

现在我在codeigniter中看到供应商文件夹

public_html/vendor/maxmind
public_html/vendor/maxmind-db
public_html/vendor/geoip2

遵循文档,我从以下位置复制所有文件:公共_html/供应商/地理位置ip 2/地理位置ip 2/源代码/

public_html/application/third_party/GeoIp2/vendor/GeoIp2/

在这里我把文件夹和文件从src目录:

Database Exception Model Record WebService ProviderInterface.php Util.php

现在,我在/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;
    }

}

然后我得到错误:

application/third_party/GeoIp2/vendor/autoload.php): failed to open stream: No such file or directory

问题是我找不到任何autoload.php在public_html/vendor/geoip 2我没有看到这个文件。我检查了其他文件夹,我发现autoload.php在这里public_html/vendor/maxmind-db
所以我复制这个autoload.php并粘贴进去:

public_html/application/third_party/GeoIp2/vendor/autoload.php

刷新页面后,现在我看到问题:

Message: Class 'GeoIp2\Database\Reader' not found

我查过了,我看到:

public_html/application/third_party/GeoIp2/vendor/GeoIp2/Database/Reader.php

我还将public_html/vendor/maxmind-db内容从src.复制到
applicaiton/third_party/GeoIp2/vendor/MaxMind,但这仍然无法解决问题。
总结一下,目前我有这样的结构:

/application
├── third_party
│   ├── GeoIp2
│   │   ├── vendor
│   │   ├── composer.json
│   │   │ ├── GeoIp2/Database Exception Model Record WebService ProviderInterface.php Util.php
│   │   │ ├── MaxMind/DB/Reader  Reader.php
│   │   │ ├── autoload.php
gwbalxhn

gwbalxhn1#

您应该安装GeoIP2包:

composer require geoip2/geoip2

相关问题