我有一个使用 XML 的地理定位 api,这里是示例:http://freegeoip.net/xml/31.44.188.50
我在我的网站上使用了这个 XML api,所以我做了一个地理位置扫描仪,但由于某些原因,当它输出时,IP/国家/邮政编码等。它并排显示,这令人困惑,我的问题是如何让输出低于彼此或在表格中。
我的代码:
<input type="text" name="username" style="text-align: center;" placeholder=" IP address" />
<input type="submit" name="submit" class="" value="Resolve" />
.`
if (isset($_POST['submit'])) {
$name = $_POST['username'];
$blacklist = array("100.100.100","123.123.123");
$ip = $_SERVER['REMOTE_ADDR'];
if(in_array($name,$blacklist))
{
echo "No";
else
$api1 = file_get_contents("http://freegeoip.net/xml/$name");
echo "<div style='display: block; color: red; font-weight: bold;>' $api1 </div>";
}
}
?>
它看起来是这样的:31.44.188.50 NL Netherlands 07 Noord-Holland Amsterdam 52.35 4.9167
这就是我想要的:
- IP 地址:31.44.188.50
- 国家:荷兰荷兰
- 城市:Noord-Holland Amsterdam
等等
最佳答案
使用 SimpleXML:
$xml = simplexml_load_file("http://freegeoip.net/xml/31.44.188.50");
echo 'IP : ', $xml->Ip, '<br>';
echo 'Country code : ', $xml->CountryCode, '<br>';
echo 'Country name : ', $xml->CountryName, '<br>';
echo 'City : ', $xml->City, '<br>';
输出:
IP : 31.44.188.50
Country code : NL
Country name : Netherlands
City : Amsterdam
有关文档的更多示例和解释: http://php.net/manual/en/book.simplexml.php
关于php - XML 输出并排显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25813233/