本文操作环境: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-cnrn" .
            "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转为城市的详细内容,更多请关注悠悠之家其它相关文章!

点赞(28)

评论列表共有 0 条评论

立即
投稿
返回
顶部