最新消息:

PHP函数第9款:PHP利用腾讯ip分享计划获取ip地理位置getIPLoc_QQ

PHP扩展函数 杨红伟 4217浏览 0评论

一:函数介绍

我们知道了IP了,但不知道我们在哪里,比如我现在太原。我们通过什么方式能知道,我到底在哪里呢?腾讯的IP分享计划,就可以帮助我们获取这一方面的知识。

演示地址:http://www.ijquery.cn/phpfunction/getIPLoc_QQ/demo1.php

下载地址: http://www.ijquery.cn/phpfunction/getIPLoc_QQ/getIPLoca_QQ.zip

二:函数应用

echo "您所输入的202.99.192.66的IP地址为:".getIPLoc_QQ('202.99.192.66');

输出的结果是: 您所输入的202.99.192.66的IP地址为:中国山西省太原市 联通

三:函数源码

//php利用腾讯ip分享计划获取ip地理位置
function getIPLoc_QQ($queryIP) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'http://ip.qq.com/cgi-bin/searchip?searchip1=' . $queryIP);
	curl_setopt($ch, CURLOPT_ENCODING, 'gb2312');
	curl_setopt($ch, CURLOPT_FAILONERROR, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	$result = curl_exec($ch);
	$result = mb_convert_encoding($result, "utf-8", "gb2312");
	curl_close($ch);

	preg_match("@<span>(.*)</span></p>@iU", $result, $ipArray);
	$gip = $ipArray[1];
	return $gip;
}

四:函数分析

其实我们就是根据 输出的结果 http://ip.qq.com/cgi-bin/searchip?searchip1=202.99.192.66 输出的结果进行分析!

五:函数图片


Warning: Use of undefined constant PRC - assumed 'PRC' (this will throw an Error in a future version of PHP) in /www/wwwroot/www.ijquery.cn/wp-content/themes/d8-3.0/comments.php on line 17
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址