Loading

获取公网ip,获取用户城市地址

<?php


class GetIp
{
    public static $api = 'http://ip.taobao.com/service/getIpInfo.php?ip=';

    public static function getAddr()
    {
        $ip = $_SERVER['REMOTE_ADDR'];
        $url = self::$api . $ip;
        $json = self::curl($url);
        return $json;
    }

    public static function getClientIp()
    {
        // 1 创建套接字
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

        //2 开启一个套接字连接
        socket_connect($socket, 'ns1.dnspod.net', 6666);

        // 3 从这个套接字中读取数据
        $buf = socket_read($socket, 15);

        // 4 关闭套接字
        socket_close($socket);

        $url = self::$api . $buf;
        $json = self::curl($url);
        return $json;
    }

    public static function curl($url)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $str = curl_exec($ch);

        curl_close($ch);

        return $str;
    }
}

    echo GetIp::getClientIp();

 ?>

 

posted @ 2016-11-05 22:56  哈利路亚健儿奋起步伐  阅读(469)  评论(0编辑  收藏  举报