Reputation: 421
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>$</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
Upvotes: 0
Views: 793
Reputation: 244
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);
Upvotes: 1
Reputation: 95161
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>₦</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
:)
Upvotes: 1
Reputation: 5249
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'];
Upvotes: 1
Reputation: 5249
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);
Upvotes: 1
Reputation: 7525
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);
Upvotes: 2