如何解析 HostIP 地理定位 API 的输出?

发布于 2024-10-26 12:21:18 字数 289 浏览 1 评论 0原文

如果我通过 http://api.hostip.info/get_html 访问 HostIP 地理定位 API .php?ip=193.148.1.1,它返回三行文本:

Country: SPAIN (ES)
City: (Unknown city)
IP: 193.148.1.1

如何在 PHP 中解析该输出以提取国家/地区名称?

If I access the HostIP geolocation API via http://api.hostip.info/get_html.php?ip=193.148.1.1, it returns three lines of text:

Country: SPAIN (ES)
City: (Unknown city)
IP: 193.148.1.1

How can I parse that output in PHP to extract the country name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

酒几许 2024-11-02 12:21:18

尝试这些 preg_matches

$info = "Country: SPAIN (ES)
City: (Unknown city)
IP: 193.148.1.1";

preg_match("/Country: (.*)\n/", $info, $out);
echo $out[1];

## OR
preg_match ("/Country: (.*) \(.*\)?\n/", $info, $out);
echo $out[1];

Try these preg_matches

$info = "Country: SPAIN (ES)
City: (Unknown city)
IP: 193.148.1.1";

preg_match("/Country: (.*)\n/", $info, $out);
echo $out[1];

## OR
preg_match ("/Country: (.*) \(.*\)?\n/", $info, $out);
echo $out[1];
深爱不及久伴 2024-11-02 12:21:18

PHP 的一些正则表达式会有帮助吗?

if (preg_match('/Country: (.*[^\n\r])/i', $subject, $regs)) {
    $result = $regs[1];
} else {
    $result = "";
}

You will have: 

Match 1: Country: SPAIN (ES)
Group 1: SPAIN (ES)

Would some regex for PHP help?

if (preg_match('/Country: (.*[^\n\r])/i', $subject, $regs)) {
    $result = $regs[1];
} else {
    $result = "";
}

You will have: 

Match 1: Country: SPAIN (ES)
Group 1: SPAIN (ES)
執念 2024-11-02 12:21:18

如果您使用以下 URL:

http://api.hostip.info/?ip=193.148.1.1

而不是:

http://, 您可以从 hostip.info 获取 XML 响应api.hostip.info/get_html.php?ip=193.148.1.1

然后,您可以解析 XML,它比正则表达式更干净,并且可能更不受输出格式可能发生的变化的影响。

这是解析输出的示例:

$response = file_get_contents('http://api.hostip.info/?ip=193.148.1.1');

$xml = new DOMDocument();
$xml->loadXml($response);

$xpath = new DOMXpath($xml);

$path = '/HostipLookupResultSet/gml:featureMember/Hostip/';

$ip = $xpath->evaluate($path . 'ip')->item(0)->nodeValue;
$city = $xpath->evaluate($path . 'gml:name')->item(0)->nodeValue;
$countryName = $xpath->evaluate($path . 'countryName')->item(0)->nodeValue;
$countryAbbrev = $xpath->evaluate($path . 'countryAbbrev')->item(0)->nodeValue;

You can obtain an XML response from hostip.info if you use the following URL:

http://api.hostip.info/?ip=193.148.1.1

instead of:

http://api.hostip.info/get_html.php?ip=193.148.1.1

Then, you can parse the XML which is kind of cleaner than Regex, and probably more immune to the possible changes of output formatting.

This is an example of parsing the output:

$response = file_get_contents('http://api.hostip.info/?ip=193.148.1.1');

$xml = new DOMDocument();
$xml->loadXml($response);

$xpath = new DOMXpath($xml);

$path = '/HostipLookupResultSet/gml:featureMember/Hostip/';

$ip = $xpath->evaluate($path . 'ip')->item(0)->nodeValue;
$city = $xpath->evaluate($path . 'gml:name')->item(0)->nodeValue;
$countryName = $xpath->evaluate($path . 'countryName')->item(0)->nodeValue;
$countryAbbrev = $xpath->evaluate($path . 'countryAbbrev')->item(0)->nodeValue;
梦幻的心爱 2024-11-02 12:21:18

http://api.hostip.info/get_json.php?position=true

{"country_name":"VENEZUELA","country_code":"VE","city":"加拉加斯","ip":"xxx.xx.xxx.xx","lat":"x.xxx"," lng":"-xx.xxxx"}

http://api.hostip.info/get_json.php?position=true

{"country_name":"VENEZUELA","country_code":"VE","city":"Caracas","ip":"xxx.xx.xxx.xx","lat":"x.xxx","lng":"-xx.xxxx"}

暗地喜欢 2024-11-02 12:21:18
function _get_country_from_IP($ip) {
  $data = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
  return $country = isset($data->country) ? $data->country : 'UNKNOWN';
}
function _get_country_from_IP($ip) {
  $data = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
  return $country = isset($data->country) ? $data->country : 'UNKNOWN';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文