Home  >  Article  >  Backend Development  >  How to use php to obtain region instance sharing through IP address

How to use php to obtain region instance sharing through IP address

*文
*文Original
2017-12-25 10:51:551709browse

Many projects will need to obtain the user's geographical information, but sometimes it cannot be obtained directly through the geographical location interface, so what should we do? This article mainly introduces the method of using PHP to obtain the IP address and region through Chianz.com, which is a very practical technique for parsing IP addresses and regions.

The example code is as follows:

<!DOCTYPE html>  
<html>  
<head>  
<title></title>  
<meta charset="UTF-8" />  
<meta name="author" content="" />  
<meta name="keywords" content="" />  
<meta name="description" content="" />  
</head>
<body>  
<?php 
$ipAddr = "4.4.44.4";  
$ipChinazAddr = "http://tool.chinaz.com/IP/?IP=".$ipAddr;  
$contents = file_get_contents($ipChinazAddr);  
$pattern = "/==>>.+</strong><br />/";  
$string = $contents;  
preg_match_all($pattern,$string,$addrArray);  
$num = count($addrArray[0]);  
$pattern = "/(==>>).+(==>>s)/";  
for($i = 0;$i<$num;$i++){  
$addrArray[0][$i] = preg_replace($pattern,"",$addrArray[0][$i]);  
}  
echo "<pre class="brush:php;toolbar:false">";  
print_r($addrArray);  
echo "
"; ?>

Related recommendations:

Get location information through WeChat and save it to the server for use

Detailed explanation of Html5 Geolocation sample code sharing for obtaining geographical location information

WeChat service account development- Get user location information


The above is the detailed content of How to use php to obtain region instance sharing through IP address. 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