代码之家  ›  专栏  ›  技术社区  ›  esafwan

从XML PHP获取信息

  •  0
  • esafwan  · 技术社区  · 15 年前

    我正在创建一个脚本,它将从用户的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>
    

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

    3 回复  |  直到 15 年前
        1
  •  1
  •   Jurian Sluiman    15 年前

    SimpleXml 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
    
        2
  •  1
  •   Coin_op    15 年前

    $xml = simplexml_load_file($xmlFile,'SimpleXMLElement', LIBXML_NOCDATA);
    $ip = (string) $xml->Ip;
    $status = (string) $xml->Status;
    
        3
  •  0
  •   jyggen    15 年前
    推荐文章