how to load xml from this code please

会有一股神秘感。 提交于 2019-12-12 02:22:10

问题


I want to load the inside values from this xml code:

    <?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>

If you can see the geoplugin_city, etc I want to load these values to php

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

that didn't work. please help me out.

opps php i ment xml


回答1:


Change your code to this first

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

After that, make a Foreach loop to have all the data from that.

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

Now, print the array and test the values.

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

However, try for dumping the variables so you will get the data types too..

var_dump($xml);



回答2:


It's not outputting XML, it's outputting serialized PHP data:

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



回答3:


You will want to use the XML api instead of the PHP api.

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



回答4:


If you want the values in PHP, I would recommend not using the XML library at all.

$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'];



回答5:


if you want to call the xml API the URL is

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

Output Sample

<?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>

EDIT 1

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

Output

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

Thanks

:)



来源:https://stackoverflow.com/questions/9998475/how-to-load-xml-from-this-code-please

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!