/**
* 获取ip地址
*
* @ApiMethod (POST)
*/
public function getIp()
{
$result = [];
$ip = false;
if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
$ip = $_SERVER["HTTP_CLIENT_IP"];
};
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode(", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip) {
array_unshift($ips, $ip);
$ip = FALSE;
};
for ($i = 0; $i < count($ips); $i++) {
if (!eregi("^(10│172.16│192.168).", $ips[$i])) {
$ip = $ips[$i];
break;
};
};
};
$result['ip'] = $ip ? $ip : $_SERVER['REMOTE_ADDR'];
$result['ip_area'] = $this->ipArea($result['ip']);
$this->success("success", $result, 200);
}
/**
* 获取ip所属地
*
* @ApiMethod (POST)
*/
public function ipArea($ip)
{
$url = 'http://whois.pconline.com.cn/ipJson.jsp?ip=' . $ip . '&json=true';
$body = $this->Go($url);
$body = iconv("GB2312", "UTF-8//IGNORE", $body);
$body = json_decode($body, true);
$address = $body['addr'];//$body['pro'].$body['city']
return $address;
}
public function Go($url)
{
$ch = curl_init();
//随机生成IP
$ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255); // 百度 蜘蛛
$timeout = 15;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
//伪造百度 蜘蛛IP
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:' . $ip . '', 'CLIENT-IP:' . $ip . ''));
//伪造百度 蜘蛛头部
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Baiduspider/2.0;+http://www.baidu.com/search/spider.html)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$content = curl_exec($ch);
curl_close($ch);//关闭一打开的会话
return $content;
}