php怎么将ip转为城市

php怎么将ip转为城市,第1张

php怎么将ip转为城市

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
$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转为城市的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/684781.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-20
下一篇 2022-04-20

发表评论

登录后才能评论

评论列表(0条)

保存