请说明如何从这段代码中加载xml


how to load xml from this code please

我想从这个xml代码中加载内部值:

    <?xml version="1.0" encoding="UTF-8"?>
<geoPlugin>
    <geoplugin_city>Salt Lake City</geoplugin_city>
    <geoplugin_region>UT</geoplugin_region>
    <geoplugin_areaCode>801</geoplugin_areaCode>
    <geoplugin_dmaCode>770</geoplugin_dmaCode>
    <geoplugin_countryCode>US</geoplugin_countryCode>
    <geoplugin_countryName>United States</geoplugin_countryName>
    <geoplugin_continentCode>NA</geoplugin_continentCode>
    <geoplugin_latitude>40.700199127197</geoplugin_latitude>
    <geoplugin_longitude>-111.94339752197</geoplugin_longitude>
    <geoplugin_regionCode>UT</geoplugin_regionCode>
    <geoplugin_regionName>Utah</geoplugin_regionName>
    <geoplugin_currencyCode>USD</geoplugin_currencyCode>
    <geoplugin_currencySymbol>&#36;</geoplugin_currencySymbol>
    <geoplugin_currencyConverter>1</geoplugin_currencyConverter>
</geoPlugin>

如果你能看到地质公园等我想将这些值加载到php

$location = 'http://www.geoplugin.net/xml.gp?ip='.$_SERVER['REMOTE_ADDR'];
$xml = simplexml_load_file($location);

那没用。请帮帮我。

opps php i ment xml

它不是输出XML,而是输出序列化的PHP数据:

$location = 'http://www.geoplugin.net/php.gp?ip=192.168.1.1';
$file = unserialize(file_get_contents($location));
print_r($file);

将代码更改为第一个

$location = 'http://www.geoplugin.net/xml.gp?ip='.$_SERVER['REMOTE_ADDR'];
$xml = simplexml_load_file($location);

之后,制作一个Foreach循环,以获得其中的所有数据。

foreach ($xml as $keys => $values) {
     $data[] = $values; // $data array have all your data independently.
}

现在,打印数组并测试值。

echo "<pre>"; print_r($data); echo "</pre>";

但是,请尝试转储变量,这样您也可以获得数据类型。。

var_dump($xml);

您将希望使用XML api而不是PHP api。

$location = 'http://www.geoplugin.net/xml.gp?ip='.$_SERVER['REMOTE_ADDR'];
$xml = simplexml_load_file($location);

如果您想要PHP中的值,我建议您不要使用XML库。

$data = unserialize(file_get_contents('http://www.geoplugin.net/php.gpip='.$_SERVER['REMOTE_ADDR'])));
//Then access the data directly.
echo $data['geoplugin_city'];
echo $data['geoplugin_region'];

如果您想调用xml API,URL是

$location = 'http://www.geoplugin.net/xml.gp?ip='.$_SERVER['REMOTE_ADDR'];
$xml = simplexml_load_file($location);

输出样本

<?xml version="1.0" encoding="UTF-8"?>
<geoPlugin>
    <geoplugin_city>Lagos</geoplugin_city>
    <geoplugin_region>Lagos</geoplugin_region>
    <geoplugin_areaCode>0</geoplugin_areaCode>
    <geoplugin_dmaCode>0</geoplugin_dmaCode>
    <geoplugin_countryCode>NG</geoplugin_countryCode>
    <geoplugin_countryName>Nigeria</geoplugin_countryName>
    <geoplugin_continentCode>AF</geoplugin_continentCode>
    <geoplugin_latitude>6.4531002044678</geoplugin_latitude>
    <geoplugin_longitude>3.395800113678</geoplugin_longitude>
    <geoplugin_regionCode>05</geoplugin_regionCode>
    <geoplugin_regionName>Lagos</geoplugin_regionName>
    <geoplugin_currencyCode>NGN</geoplugin_currencyCode>
    <geoplugin_currencySymbol>&#8358;</geoplugin_currencySymbol>
    <geoplugin_currencyConverter>157.6899963379</geoplugin_currencyConverter>
</geoPlugin>

编辑1

echo "<pre>";
foreach($xml as $key => $value)
{
    echo $key , " = " , $value , "'n" ;
}

输出

geoplugin_city = Lagos
geoplugin_region = Lagos
geoplugin_areaCode = 0
geoplugin_dmaCode = 0
geoplugin_countryCode = NG
geoplugin_countryName = Nigeria
geoplugin_continentCode = AF
geoplugin_latitude = 6.4531002044678
geoplugin_longitude = 3.395800113678
geoplugin_regionCode = 05
geoplugin_regionName = Lagos
geoplugin_currencyCode = NGN
geoplugin_currencySymbol = ₦
geoplugin_currencyConverter = 157.6899963379

感谢

:)