从 xml php 获取信息

发布于 2024-09-29 06:26:26 字数 906 浏览 0 评论 0原文

我正在创建一个脚本,该脚本将从用户的 IP 获取位置。 我看到了这个 api。但知道如何将它们放入数组并打印它们。

这是一个示例链接: http://ipinfodb.com/ip_query.php?ip=74.125。 45.100&timezone=true

这是一个示例响应,

<Response>
<Ip>74.125.45.100</Ip>
<Status>OK</Status>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
<RegionCode>06</RegionCode>
<RegionName>California</RegionName>
<City>Mountain View</City>
<ZipPostalCode>94043</ZipPostalCode>
<Latitude>37.4192</Latitude>
<Longitude>-122.057</Longitude>
<TimezoneName>America/Los_Angeles</TimezoneName>
<Gmtoffset>-25200</Gmtoffset>
<Isdst>1</Isdst>
</Response>

我如何从数组中获取信息并打印?

I'm creating a script that will take users location from their ip.
I saw this api.But know idea how i can get them in array and print them.

Here is a sample link:
http://ipinfodb.com/ip_query.php?ip=74.125.45.100&timezone=true

This a sample respones

<Response>
<Ip>74.125.45.100</Ip>
<Status>OK</Status>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
<RegionCode>06</RegionCode>
<RegionName>California</RegionName>
<City>Mountain View</City>
<ZipPostalCode>94043</ZipPostalCode>
<Latitude>37.4192</Latitude>
<Longitude>-122.057</Longitude>
<TimezoneName>America/Los_Angeles</TimezoneName>
<Gmtoffset>-25200</Gmtoffset>
<Isdst>1</Isdst>
</Response>

How do i get the information from this in an array and print?

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

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

发布评论

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

评论(3

獨角戲 2024-10-06 06:26:26

使用 SimpleXml 实例并循环遍历所有 子节点

$url = 'http://ipinfodb.com/ip_query.php?ip=74.125.45.100&timezone=true';
$xml = new SimpleXmlElement($url, null, true);

$info = array();
foreach ($xml->children() as $child) {
    $name = $child->getName();
    $info[$name] = $xml->$name;
}

// info['Ip'] = '74.125.45.100', etc

Use a SimpleXml instance and loop through all the children nodes

$url = 'http://ipinfodb.com/ip_query.php?ip=74.125.45.100&timezone=true';
$xml = new SimpleXmlElement($url, null, true);

$info = array();
foreach ($xml->children() as $child) {
    $name = $child->getName();
    $info[$name] = $xml->$name;
}

// info['Ip'] = '74.125.45.100', etc
兮子 2024-10-06 06:26:26

您应该查看 simpleXML (http://php.net/manual/en/book.simplexml.php)

使用 simpleXML 您可以执行以下操作:

$xml = simplexml_load_file($xmlFile,'SimpleXMLElement', LIBXML_NOCDATA);
$ip = (string) $xml->Ip;
$status = (string) $xml->Status;

You should look into simpleXML (http://php.net/manual/en/book.simplexml.php)

Using simpleXML you could do something like:

$xml = simplexml_load_file($xmlFile,'SimpleXMLElement', LIBXML_NOCDATA);
$ip = (string) $xml->Ip;
$status = (string) $xml->Status;
云仙小弟 2024-10-06 06:26:26

看看 SimpleXML,它是用 PHP 解析 XML 的最简单方法。

Look at SimpleXML, it's the easiest way to parse XML with PHP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文