The following tutorial column of Laravel Framework will introduce to you the method of obtaining the names of provinces and cities based on IP. I hope it will be helpful to friends in need!
Get geolocation based on ip
, I tried the torann/geoip
package of laravel
, but it’s not Very accurate. There is also Baidu API
to call the method of obtaining geographical information. In order to reduce maintenance costs, I finally used a package in thinkphp3.2
. It is not clear whether it comes with the TP framework. The principle of this package to obtain positioning is the built-in provincial and municipal database UTFWry.dat
.
I placed it in the same level directory of the projectapp
sdk
Down. In order to be used, IpLocation.class.php
needs to be added to classmap
autoloading in composer.json
so that the classes contained in this file can be It is automatically loaded when called (if you don't understand why it can be loaded automatically, refer to the Laravel Composer automatic loading principle).
"autoload": { "classmap": [ "database/seeds", "database/factories", "sdk/Org/Net/IpLocation.class.php" ]},
Execute after addingcomposer dump-autoload
$ip = new \Org\Net\IpLocation('UTFWry.dat'); $location = $ip->getlocation('223.104.1.100'); dd($location);
1. Standard return valueLet’s take the IP of Shenzhen, Guangdong as an example
# 打印结果 array:5 [ "ip" => "223.104.1.100" "beginip" => "223.104.1.0" "endip" => "223.104.1.255" "country" => "广东省深圳市" "area" => "移动"]
2. The return values are different in individual regions. The left side is the demonstration IP and the right side is the returned region name
# 直辖市 '223.104.3.155',//北京市 '223.104.7.155',//天津市 '223.104.25.155',//重庆市 '223.104.5.200',//上海市徐汇区 # 自治区 '223.104.15.100',//内蒙古呼和浩特市 '223.104.29.100',//宁夏银川市 '223.104.30.100',//新疆乌鲁木齐市 '211.139.74.100',//西藏拉萨市 '218.204.63.100',//广西百色市 '1.32.192.100',//香港 '60.246.49.100',//澳门
app\Helpers.php,
<?php function get_ip_location($ip){ $ip_driver = new \Org\Net\IpLocation('UTFWry.dat'); $location = $ip_driver->getlocation($ip); $location = $location['country']; //广东省深圳市 // 如果没有查询到的默认返回值 $default = ['p_id'=>1,'c_id'=>0,'locname'=>'北京']; // 如果有市 那么市后边的字符删除 只保留到市 $city_strpos = mb_strpos($location, '市'); if($city_strpos){ $location = mb_substr($location, 0, $city_strpos + 1); } // 如果是直辖市,若匹配到直接返回,不继续匹配 “区” $spacial = ['北京','上海','天津','重庆'];//北京市/天津市/重庆市/上海市徐汇区 foreach ($spacial as $bj) { if( strpos($location, $bj) !== false ){ $province_name = $bj; $province_id = DB::table('loc_province')->where('name',$province_name)->value('province_id'); return ['p_id'=>$province_id,'c_id'=>0,'locname'=>$bj.'市']; } } // 其他标准查询 $expect_ids = [36,37,38,39,69]; //排除北京、、以及想要排除的地区 $province_name = $city_name = ''; //初始化 $province_id = $city_id = 0; // 所有省数据 $all_province = DB::table('loc_province')->whereNotIn('id', $expect_ids)->get(); foreach ($all_province as $prov) { $name = $prov->name; $prov_pos = mb_strpos($location, $name); // 如果匹配到目标 if( $prov_pos !== false ){ $province_id = $prov->province_id; $province_name = $name; // 从字符串中去除省名称,并把省字去掉 $location = mb_substr($location, $prov_pos + mb_strlen($name)); $location = str_replace('省','',$location); // 如果有“市”,那么就提取出市的名称 if($location && mb_strpos($location, '市')){ $city_name = rtrim($location, '市'); } if($city_name){ $city_id = DB::table('loc_city')->where('name',$city_name)->value('cid'); }else{ $city_id = 1; // 默认省会 } break; } } if($province_name){ return ['p_id'=>$province_id,'c_id'=>$city_id,'locname'=>$province_name.$city_name]; }else{ return $default; } }
Test
$testips = [ '223.104.3.155',//北京市 '223.104.7.155',//天津市 '223.104.25.155',//重庆市 '223.104.5.200',//上海市徐汇区 '223.104.15.100',//内蒙古呼和浩特市 '223.104.29.100',//宁夏银川市 '223.104.30.100',//新疆乌鲁木齐市 '211.139.74.100',//西藏拉萨市 '218.204.63.100',//广西百色市 '1.32.192.100',//香港 '60.246.49.100',//澳门 '223.104.1.100', //广东省深圳市 ]; echo "<pre class="brush:php;toolbar:false">"; foreach ($testips as $ip) { print_r(get_ip_location($ip)); }
Array ( [p_id] => 1 [c_id] => 0 [locname] => 北京市 ) Array ( [p_id] => 3 [c_id] => 0 [locname] => 天津市 ) Array ( [p_id] => 4 [c_id] => 0 [locname] => 重庆市 ) Array ( [p_id] => 2 [c_id] => 0 [locname] => 上海市 ) Array ( [p_id] => 7 [c_id] => 1 [locname] => 内蒙古呼和浩特 ) Array ( [p_id] => 29 [c_id] => 1 [locname] => 宁夏银川 ) Array ( [p_id] => 31 [c_id] => 1 [locname] => 新疆乌鲁木齐 ) Array ( [p_id] => 26 [c_id] => 1 [locname] => 西藏拉萨 ) Array ( [p_id] => 21 [c_id] => 13 [locname] => 广西百色 ) Array ( [p_id] => 32 [c_id] => 1 [locname] => 香港 ) Array ( [p_id] => 33 [c_id] => 1 [locname] => 澳门 ) Array ( [p_id] => 20 [c_id] => 2 [locname] => 广东深圳 )
#
The above is the detailed content of Get the province and city name based on IP in laravel project. For more information, please follow other related articles on the PHP Chinese website!