通过IP获取地理位置Ali接口
某项目微信隐形授权通过IP方式调用阿里云接口,获取地址位置。该接口为付费接口,200元1000W次,请自行到阿里云进行申请。
public function clidoingarea(){
$demoDB = M('表名');
$where['city'] = array('EXP','IS NULL'); //查询条件城市为空
$where['region'] = array('EXP','IS NULL'); //查询条件省份为空
$nolist = $demoDB->where($where)->limit(50)->select(); //每次取50条记录
foreach($nolist as $key => $val){
$arealist = json_decode($this->getQuery($val['ip']),true); //调用地理位置IP接口
// 进行城市判断 有则录入 没有则未知 确保数据可以只调用一遍
if($arealist['data']['city']){
$saveArr['city'] = $arealist['data']['city'];
}else{
$saveArr['city'] = "未知";
}
// 进行省份判断 有则录入 没有则未知 确保数据可以只调用一遍
if($arealist['data']['region']){
$saveArr['region'] = $arealist['data']['region'];
}else{
$saveArr['region'] = "未知";
}
// 根据IP保存
$demoDB->where("ip ='" . $val['ip'] . "'")->save($saveArr);
}
}
public function getQuery($ip) {
$host = "https://api01.aliyun.venuscn.com";
$path = "/ip";
$method = "GET";
$appcode = "appcode值";
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
$querys = "ip=".$ip;
$bodys = "";
$url = $host . $path . "?" . $querys;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
if (1 == strpos("$".$host, "https://"))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$ret = curl_exec($curl);
return $ret;
}
复制到项目中的方法中后,增加服务器的定时任务。该程序是按照每分钟50条的速度进行编译。
定时任务设置过程:
crontab -e
编辑定时任务
*/1 * * * * wget http://dyn.easy-all.cn/定时任务方法
每分钟以wget方式访问URL
:wq
保存退出
service crond reload
重新加载定时任务