简体   繁体   English

从xml php获取信息

[英]Getting information from xml php

I'm creating a script that will take users location from their ip. 我正在创建一个脚本,将用户的位置从他们的IP。 I saw this api.But know idea how i can get them in array and print them. 我看到了这个api.But知道如何将它们放入数组并打印出来。

Here is a sample link: http://ipinfodb.com/ip_query.php?ip=74.125.45.100&timezone=true 以下是一个示例链接: 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? 如何从阵列中获取信息并打印?

You should look into simpleXML (http://php.net/manual/en/book.simplexml.php) 您应该研究simpleXML(http://php.net/manual/zh/book.simplexml.php)

Using simpleXML you could do something like: 使用simpleXML,您可以执行以下操作:

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

Use a SimpleXml instance and loop through all the children nodes 使用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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM