通過api獲取訪問的ip來源
<?php
/**
* ip search
* Created by PhpStorm.
* User: saint
* Date: 14-10-23
* Time: 下午4:28
*/
class
ip_lib
{
// 淘寶ip地址庫
public
function
get_ip_info_taobao(
$ip
)
{
$server_api
=
'http://ip.taobao.com/service/getIpInfo.php?ip='
;
$uri
=
$server_api
.
$ip
;
$json_string
=
$this
->get_remote_data(
$uri
);
$array
= json_decode(
$json_string
, true);
$ret
=
array
();
if
(
$array
[
'code'
])
{
$ret
=
array
(
'country'
=>
'未知'
,
'city'
=>
'未知'
,
'isp'
=>
'未知'
);
}
else
{
$ret
[
'country'
] =
$array
[
'data'
][
'country'
] ?
$array
[
'data'
][
'country'
] :
'未知'
;
$ret
[
'city'
] =
$array
[
'data'
][
'city'
] ?
$array
[
'data'
][
'city'
] :
'未知'
;
$ret
[
'isp'
] =
$array
[
'data'
][
'isp'
] ?
$array
[
'data'
][
'isp'
] :
'未知'
;
}
return
$ret
;
}
// 新浪ip地址庫
public
function
get_ip_info_sina(
$ip
)
{
$server_api
=
'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='
;
$uri
=
$server_api
.
$ip
;
$json_string
=
$this
->get_remote_data(
$uri
);
$array
= json_decode(
$json_string
, true);
$ret
=
array
();
if
(
$array
[
'ret'
] != 1)
{
$ret
=
array
(
'country'
=>
'未知'
,
'city'
=>
'未知'
,
'isp'
=>
'未知'
);
}
else
{
$ret
[
'country'
] =
$array
[
'country'
] ?
$array
[
'country'
] :
'未知'
;
$ret
[
'city'
] =
$array
[
'province'
] ?
$array
[
'province'
] :
'未知'
;
$ret
[
'isp'
] =
$array
[
'city'
] ?
$array
[
'city'
] :
'未知'
;
}
return
$ret
;
}
private
function
get_remote_data(
$uri
)
{
$ch
= curl_init(
$uri
) ;
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, true) ;
// 獲取數據返回
curl_setopt(
$ch
, CURLOPT_BINARYTRANSFER, true) ;
// 在啟用 CURLOPT_RETURNTRANSFER 時候將獲取數據返回
return
$output
= curl_exec(
$ch
) ;
}
}
推薦文章
2025-01-18
2024-11-28
2024-11-09
2024-10-25
2024-06-25
2024-01-04
2023-11-06
2023-10-30
2023-10-13
2023-10-10
穩定
產品高可用性高并發貼心
項目群及時溝通專業
產品經理1v1支持快速
MVP模式小步快跑承諾
我們選擇聲譽堅持
10年專注高端品質開發