php将ip转为城市的方法:1、创建一个PHP示例文件;2、获取具体的IP地址;3、通过“function get_tag_data($html,$tag){...}”等方法将ip转为城市地址即可。

本文操作环境:Windows7系统,PHP7.4版,Dell G3电脑。
php怎么将ip转为城市?
PHP IP地址转城市 实际地址:
我这里使用的是http://www.cip.cc/查询IP实际地址,暂时性的解决了我的问题( http://www.cip.cc/ 网站废了这个方法就废了,慎用)
立即学习“PHP免费学习笔记(深入)”;
<?php
$ip = '具体的IP地址';
$a = 'http://www.cip.cc/'.$ip;
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>
"Accept-language: zh-cn\r\n" .
"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; 4399Box.560; .NET4.0C; .NET4.0E)" .
"Accept: *//*"
)
);
$ctx = stream_context_create($opts);
$x = file_get_contents($a,false,$ctx);
$result = get_tag_data($x,"pre");
function get_tag_data($html,$tag){
$regex = "/<$tag>(.*?)<\/$tag>/is";
preg_match_all($regex,$html,$matches,PREG_PATTERN_ORDER);
return $matches[1];//返回值为数组 ,查找到的标签内的内容
}推荐学习:《PHP视频教程》
以上就是php怎么将ip转为城市的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号