码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-11-06 01:50:09      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:.com   class   sel   com   code   读取数据   info   tcp   ret   

<?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();

 ?>

技术分享

 

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

标签:.com   class   sel   com   code   读取数据   info   tcp   ret   

原文地址:http://www.cnblogs.com/arvintang/p/6034323.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!