Home >Backend Development >PHP Tutorial >PHP does not need an IP library, get the city name to use! Use of php escape characters Use of the Guardian PHP suite Use of php source code

PHP does not need an IP library, get the city name to use! Use of php escape characters Use of the Guardian PHP suite Use of php source code

WBOY
WBOYOriginal
2016-07-29 08:54:131102browse

In the latest secondary development, we need to develop the current city to enter automatic selection, so we thought of a way to use PHP without an IP library to get the city name for use!
You can use the Innocence IP library to obtain user city information, just However, the city entry function I am currently developing seems to be like Meituan. It seems that the IP library cannot be used directly. So we can only steal other people’s IP library information..
I temporarily provide two external IP library information to obtain, which is relatively stable.
Sina interface address: http://int.dpool.sina.com.cn/iplookup/iplookup.php
Taobao API interface: http://ip.taobao.com/service/getIpInfo.php?ip=125.73.111.159
We want to get one of the external IP city library information above

Use PHP’s file_get_contents to get it

<span style="font-size:18px;">//php脚本开始
	//PHP返回客户端IP,还不错
	function GetIP(){
	if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
	$ip = getenv("HTTP_CLIENT_IP");
	else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
	$ip = getenv("HTTP_X_FORWARDED_FOR");
	else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
	$ip = getenv("REMOTE_ADDR");
	else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
	$ip = $_SERVER['REMOTE_ADDR'];
	else
	$ip = "unknown";
	return($ip);
	}
	//如果是本地IP,将无法测试,如要测试,开启下面的代码就会返回我的IP
	/**/
	function GetIPs(){return '125.73.111.159';}
	/**/
	//远程城市数据返回点
	$filename = "http://ip.taobao.com/service/getIpInfo.php?ip=".GetIPs();
	//使用file_get_contents返回json数据,确认程序是否开启file_get_contents
	$json = json_decode(file_get_contents($filename));
	//转换编码,不然会乱码
	$city=iconv("utf-8","gb2312",$json->data->city);
	//打印城市
	 
	echo $city;
	//到这里我们就可以使用SQL查询与数据库的城市进行匹配了,返回我们需要的城市</span>

In this way, we can get the city name for use without the need for an IP library!

Original address: http://www.12345t.com/code/php/20130819/70.html

The above introduces PHP without an IP library, getting the city name to use!, including PHP, usage content, I hope it will be helpful to friends who are interested in PHP tutorials.

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