Home  >  Article  >  PHP Framework  >  Get the province and city name based on IP in laravel project

Get the province and city name based on IP in laravel project

藏色散人
藏色散人forward
2020-12-11 13:42:492008browse

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 the province and city name based on IP in laravel project

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.

根据 IP 获取省市名称

How to use the laravel project

I placed it in the same level directory of the projectappsdk 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

##Basic use
$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',//澳门

Encapsulate a method function that returns the region id

If you use it frequently, you generally like to place it in

app\Helpers.php,

<?php

function get_ip_location($ip){
    $ip_driver = new \Org\Net\IpLocation(&#39;UTFWry.dat&#39;);
    $location = $ip_driver->getlocation($ip);
    $location = $location[&#39;country&#39;]; //广东省深圳市

    // 如果没有查询到的默认返回值
    $default = [&#39;p_id&#39;=>1,&#39;c_id&#39;=>0,&#39;locname&#39;=>&#39;北京&#39;];

    // 如果有市 那么市后边的字符删除 只保留到市
    $city_strpos = mb_strpos($location, &#39;市&#39;);
    if($city_strpos){
        $location = mb_substr($location, 0, $city_strpos + 1);
    }

    // 如果是直辖市,若匹配到直接返回,不继续匹配 “区”
    $spacial = [&#39;北京&#39;,&#39;上海&#39;,&#39;天津&#39;,&#39;重庆&#39;];//北京市/天津市/重庆市/上海市徐汇区
    foreach ($spacial as $bj) {
        if( strpos($location, $bj) !== false ){
            $province_name = $bj;
            $province_id = DB::table(&#39;loc_province&#39;)->where(&#39;name&#39;,$province_name)->value(&#39;province_id&#39;);
            return [&#39;p_id&#39;=>$province_id,&#39;c_id&#39;=>0,&#39;locname&#39;=>$bj.&#39;市&#39;];
        }
    }

    // 其他标准查询
    $expect_ids = [36,37,38,39,69]; //排除北京、、以及想要排除的地区
    $province_name = $city_name = &#39;&#39;; //初始化
    $province_id = $city_id = 0;
    // 所有省数据
    $all_province = DB::table(&#39;loc_province&#39;)->whereNotIn(&#39;id&#39;, $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(&#39;省&#39;,&#39;&#39;,$location);
            // 如果有“市”,那么就提取出市的名称
            if($location && mb_strpos($location, &#39;市&#39;)){
                $city_name = rtrim($location, &#39;市&#39;);
            }
            if($city_name){
                $city_id = DB::table(&#39;loc_city&#39;)->where(&#39;name&#39;,$city_name)->value(&#39;cid&#39;);
            }else{
                $city_id = 1; // 默认省会
            }

            break;
        }
    }

    if($province_name){
        return  [&#39;p_id&#39;=>$province_id,&#39;c_id&#39;=>$city_id,&#39;locname&#39;=>$province_name.$city_name];
    }else{
        return $default;
    }
}

Test

$testips = [
    &#39;223.104.3.155&#39;,//北京市 
    &#39;223.104.7.155&#39;,//天津市 
    &#39;223.104.25.155&#39;,//重庆市 
    &#39;223.104.5.200&#39;,//上海市徐汇区 

    &#39;223.104.15.100&#39;,//内蒙古呼和浩特市 
    &#39;223.104.29.100&#39;,//宁夏银川市 
    &#39;223.104.30.100&#39;,//新疆乌鲁木齐市 
    &#39;211.139.74.100&#39;,//西藏拉萨市 
    &#39;218.204.63.100&#39;,//广西百色市 

    &#39;1.32.192.100&#39;,//香港 
    &#39;60.246.49.100&#39;,//澳门

    &#39;223.104.1.100&#39;, //广东省深圳市
];
echo "<pre class="brush:php;toolbar:false">";
foreach ($testips as $ip) {
    print_r(get_ip_location($ip));
}

Check results

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] => 广东深圳
)

Original address: https://learnku.com/articles/52456

#                                                                                                     

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!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete