我想从这个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>$</geoplugin_currencySymbol>
<geoplugin_currencyConverter>1</geoplugin_currencyConverter>
</geoPlugin>
如果你能看到geoplugin_city等 我想将这些值加载到php
$location = 'http://www.geoplugin.net/xml.gp?ip='.$_SERVER['REMOTE_ADDR'];
$xml = simplexml_load_file($location);
没用。 请帮帮我。
opps php i ment xml
答案 0 :(得分:2)
它不输出XML,而是输出序列化的PHP数据:
$location = 'http://www.geoplugin.net/php.gp?ip=192.168.1.1';
$file = unserialize(file_get_contents($location));
print_r($file);
答案 1 :(得分:1)
如果要调用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>₦</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
由于
:)
答案 2 :(得分:1)
您将需要使用XML api而不是PHP api。
$location = 'http://www.geoplugin.net/xml.gp?ip='.$_SERVER['REMOTE_ADDR'];
$xml = simplexml_load_file($location);
答案 3 :(得分:1)
如果你想要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'];
答案 4 :(得分:1)
将您的代码更改为第一个
$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);