Home > Backend Development > PHP Tutorial > PHP development tips: Get the city based on the IP address

PHP development tips: Get the city based on the IP address

不言
Release: 2023-03-23 17:46:01
Original
3069 people have browsed it

The content shared with you in this article is PHP development tips for obtaining the city based on the IP address. It has a certain reference value. Friends in need can refer to it


We still use this method quite a lot to facilitate the collection of information for data mining analysis. This method not only obtains the current city based on the IP address, but also obtains the user's city location based on the HTTP request.

Implementation method: Mainly obtain it based on the Amap API. First register as an Amap user, then authenticate as a developer, create an application and obtain the key for calling. The specific implementation method is as follows:


<?php

/**
 * =======================================
 * Created by ZHIHUA·WEI.
 * Author: ZHIHUA·WEI
 * Date: 2018/4/12
 * Time: 16:13
 * Project: PHP开发小技巧
 * Power: 根据ip地址获取城市
 * =======================================
 */

/**
 * 根据HTTP请求获取用户位置
 */
function get_user_location()
{
    $key = "4a218d0d82c3a74acf019b701e8c0ccc"; // 高德地图key
    $url = "http://restapi.amap.com/v3/ip?key=$key";
    $json = file_get_contents($url);
    $obj = json_decode($json, true); // 转换数组
    $obj["message"] = $obj["status"] == 0 ? "失败" : "成功";
    return $obj;
}

/**
 * 根据 ip 获取 当前城市
 */
function get_city_by_ip()
{
    if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
        $cip = $_SERVER["HTTP_CLIENT_IP"];
    } elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
        $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    } elseif (!empty($_SERVER["REMOTE_ADDR"])) {
        $cip = $_SERVER["REMOTE_ADDR"];
    } else {
        $cip = "";
    }
    $url = &#39;https://restapi.amap.com/v3/ip&#39;;
    $data = array(
        &#39;output&#39; => &#39;json&#39;,
        &#39;key&#39; => &#39;4a218d0d82c3a74acf019b701e8c0ccc&#39;,
        &#39;ip&#39; => $cip
    );

    $postdata = http_build_query($data);
    $opts = array(
        &#39;http&#39; => array(
            &#39;method&#39; => &#39;POST&#39;,
            &#39;header&#39; => &#39;Content-type: application/x-www-form-urlencoded&#39;,
            &#39;content&#39; => $postdata
        )
    );

    $context = stream_context_create($opts);

    $result = file_get_contents($url, false, $context);
    if (!empty($result)) {
        $res = json_decode($result, true);
        if (!empty($res)) {

            if (count($res[&#39;province&#39;]) == 0) {
                $res[&#39;province&#39;] = &#39;北京市&#39;;
            }
            if (!empty($res[&#39;province&#39;]) && $res[&#39;province&#39;] == "局域网") {
                $res[&#39;province&#39;] = &#39;北京市&#39;;
            }
            if (count($res[&#39;city&#39;]) == 0) {
                $res[&#39;city&#39;] = &#39;北京市&#39;;
            }
        } else {
            $res[&#39;province&#39;] = &#39;北京市&#39;;
            $res[&#39;city&#39;] = &#39;北京市&#39;;
        }

        return $res;
    } else {
        return array(
            "province" => &#39;北京市&#39;,
            "city" => &#39;北京市&#39;
        );
    }
}
Copy after login


Related recommendations:

Tips for PHP development: Determining whether it is WeChat access

The above is the detailed content of PHP development tips: Get the city based on the IP address. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template