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

PHP implements code sharing to obtain IP address location

小云云
小云云Original
2018-03-03 15:56:494170browse

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; 
}

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!

Statement:
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