Home > Backend Development > PHP Tutorial > PHP implements code sharing to obtain IP address location

PHP implements code sharing to obtain IP address location

小云云
Release: 2023-03-20 18:04:01
Original
4199 people have browsed it

This article mainly shares with you the code for obtaining the IP address location in PHP. I hope it can help you.

/**
 * 获取IP地址所在地
 */
function getIPLoc($ip)
{
    $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$ip;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回
    $result = curl_exec($ch);
    curl_close($ch);
    // $result = httpRequest($url);
    $result = json_decode($result); 
    $data = array();
    if ($result && !empty($result->province)) {
        $data['country'] = $result->country;
        $data['province'] = $result->province;
        $data['city'] = $result->city;
    } 
    return $data; 
}
Copy after login

Related recommendations:

php implementation of obtaining the geographical location of the IP address

php obtains the IP address location query program_PHP Tutorial

Based on PHP to obtain the IP address through photos, _PHP Tutorial

The above is the detailed content of PHP implements code sharing to obtain IP address location. 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