php 获取公网IP地址、定位所在城市

发布于:2020-07-17 09:27:20

本地localhost、127.0.0.1时,均能获取外网IP地址。

?php
    //获取外网IP的地址,以下两个均测试成功
    //$url = 'http://tool.huixiang360.com/zhanzhang/ipaddress.php';
    $url = 'http://city.ip138.com/ip2city.asp';

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $a  = curl_exec($ch);
    preg_match('/\[(.*)\]/', $a, $ip);
    //echo $ip[1];

    //根据IP地址定位所在城市
    ①使用淘宝IP库
    $res = @file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip");
    $res = json_decode($res,true);
    print_r($res);
    
        ②使用新浪IP库
    $res = @file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip");
    $res = json_decode($res,true);
    print_r($res); 


?>


返回结果如图:

50da1e1823f66a1a0f54dfca844cebe1.png


返回结果如图:

c26d137f9d1f60a90ef378a897c68406.png



阅读 124+

一片空白

父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌。