我正在尝试在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");我得到这个错误:"Unable to load the requested file: ProviderInterface“
我看过这个Namespace in PHP CodeIgniter Framework,但我对名称空间几乎没有经验。
这没有成功,我没有赢,我真的不知道如何在CodeIgniter中实现这一点。
发布于 2016-11-04 02:20:27
我一直在努力寻找这个问题的答案。但在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;
}我将mmdb库放在third_party文件夹中,并将GeoIP2文件放在公共文件夹中。它对我来说很好。我希望它能节省某人的时间:)
发布于 2014-06-22 06:10:28
php PHP sdk利用了GeoIp2的名称空间特性,而CodeIgniter框架并不支持这个特性,这就是为什么你在尝试加载库的时候会收到这个错误的原因。你链接的帖子提供了一个使用spl_autoload的解决方案,但是我没有使用CodeIgniter,也没有用GeopIp2 php sdk测试过它。
发布于 2015-12-10 01:42:36
有几种方法可以将其嵌入到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() ..。诸若此类
https://stackoverflow.com/questions/24345451
复制相似问题