PHP IP地址转城市 实际地址

参考文章 https://www.cnblogs.com/f-rt/p/10848201.html (PHP正则获取html任意标签,根据具体需求更改代码即可)

我这里使用的是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
" .
            "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];//返回值为数组 ,查找到的标签内的内容
}

  

原文地址:https://www.cnblogs.com/zhangpooo/p/13715886.html