我试图从输入帖子发布的数据中处理数组ip。要将此转换为纬度经度,我使用以下代码
这是我的ci库:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
include APPPATH . 'libraries/GeoIP/src/geoipcity.inc';
class GeoIp{
public function info($ip){
$gi = geoip_open(APPPATH . 'libraries/GeoIp/data/GeoLiteCity.dat', GEOIP_STANDARD);
$record = geoip_record_by_addr($gi, $ip);
geoip_close($gi);
return $record;
}
public function get($ip){
$gi = geoip_open(APPPATH . 'libraries/GeoIp/data/GeoLiteCity.dat', GEOIP_STANDARD);
$records = array();
$ips = is_array($ip);
foreach ($ips as $key=>$value) {
$records[] = geoip_record_by_addr($gi, $value);
}
foreach ($records as $record) {
$record_string = $record->longitude . "," . $record->latitude;
geoip_close($gi);
return $record_string;
}
}
}
这是我的控制器:
public function iplat(){
$this->load->library('geoip');
$record = $this->geoip->get($this->input->post('ip'));
echo json_encode($record);
}
此代码始终返回null
答案 0 :(得分:1)
我马上看到的一个问题是你的代码
is_array($ip)
由于您已将$ips
分配给$ips
,$ip
现在是布尔值而不是数组。所以你不能在它上面运行循环。也许你打算像这样运行if(is_array($id))
{
foreach ($ip as $key=>$value)
{
$records[] = geoip_record_by_addr($gi, $value);
}
}
的循环:
$('#ABC').DataTable({
"destroy": true,
language: {
processing: "<img src='" + $('#ajaxImageUrl').val() + "'/>"
},
"processing": true,
"serverSide": true,
"columnDefs": [{
"targets": 0,
"orderable": false
}],
"ajax": {
url: 'url',
type: "post",
beforeSend:function(jqXHR, settings){
if(settings.nTable.jqXHR)
settings.nTable.jqXHR.abort();
},
xhr: function() {
var xhr = $.ajaxSettings.xhr();
xhr.onreadystatechange = function(){
//code
}
xhr.upload.onprogress = function(e) {
setDesign();
xhrCount++;
};
return xhr;
},
}
});