我使用ipinfo.io进行一些简单的查找,但是echo $details->org;有一个小问题,它输出"AS15169 Google .“,但我只想要ISP部分,所以"Google .”。
示例代码:
<?
function ip_details($ip) {
$json = file_get_contents("http://ipinfo.io/{$ip}");
$details = json_decode($json);
return $details;
}
$details = ip_details($_SERVER['REMOTE_ADDR']);
echo $details->org;
?>输出示例:http://ipinfo.io/8.8.8.8/org
需要帮忙吗有人吗?
发布于 2014-12-19 02:25:46
如果您只想要org字段,您可以查询http://ipinfo.io/{$ip}/org,这将为您提供一个字符串,这将使您不必解析任何JSON:
$org = file_get_contents("http://ipinfo.io/{$ip}/org");通过在第一个空格上爆炸,我们可以将组织字符串拆分为ASN和name:
list($asn, $name) = explode(" ", $org, 2);把这一切结合在一起,我们会得到:
function org_name($ip) {
$org = file_get_contents("http://ipinfo.io/{$ip}/org");
list($asn, $name) = explode(" ", $org, 2);
return $name;
}
echo org_name("8.8.8.8");
// => Google Inc.
echo org_name("189.154.55.170");
// => Uninet S.A. de C.V.
echo org_name("172.250.147.230");
// => Time Warner Cable Internet LLC有关不同端点和速率限制的详细信息,请参阅http://ipinfo.io/developers。
发布于 2014-12-18 23:42:03
使用regex查找以AS开头并有一个或多个数字后面跟着空格的单词边界之间的任何内容,然后用空字符串替换它。
我对regex不是很在行,所以可能有人会拿出更好的解决方案。但是我在PHP Live Regex上测试了它,它适用于我尝试过的几个测试用例。
<?
function ip_details($ip) {
$json = file_get_contents("http://ipinfo.io/{$ip}");
$details = json_decode($json);
return $details;
}
$details = ip_details($_SERVER['REMOTE_ADDR']);
$org = preg_replace('/\bAS\d+\s\b/i', '', $details->org);
echo $org;
?>https://stackoverflow.com/questions/27557470
复制相似问题