How to get country name from IP address in php [duplicate] - php

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I want to find country, city, latitude and longitude from IP address using php. I am using this url and it is returning data in xml format.
http://www.ipgp.net/api/xml/122.163.6.58
The data is coming like this:
<IpLookup>
<Ip>122.163.6.58</Ip>
<Code>IN</Code>
<Country>India</Country>
<Flag>http://www.ipgp.net/flags/in.png</Flag>
<City>Calcutta</City>
<Region>West Bengal</Region>
<Isp></Isp>
<Lat>22.5697</Lat>
<Lng>88.3697</Lng>
</IpLookup>
Can anybody suggest how to parse and get the result

Use the XML parser included in PHP?

Use simplexml_load_string().

I've been using this personally:
http://ipinfodb.com/
The examples are very clear and concise and the API is very fast.
Good luck.

The use of API in PHP has already described in their website. Why you use but don't read?
http://www.ipgp.net/developer-tools/

I'll suggest you to use xpath this is more easier schema for accessing the attributes.
for example for your current data i have the following:
$file = 'file.xml';
$xml = new SimpleXMLElement($file, NULL, TRUE);
$ipz = $xml->xpath("/IpLookup/Ip");
$country = $xml->xpath("/IpLookup/Country/");
foreach($ipz as $ip)
{
foreach($country as $country)
{
echo $ip.'<br/>';
echo $country.'<br/>';
}
}
this code will return you the ip and the country for your current xml. you can edit it in your own way.

Related

How to read in XML strings as variables? [duplicate]

This question already has answers here:
A simple program to CRUD node and node values of xml file [closed]
(2 answers)
Closed 8 years ago.
Try this query:
http://www.dictionaryapi.com/api/v1/references/collegiate/xml/gobabola?key=135a6187-af83-4e85-85c1-1a28db11d5da
How do I simply read in the suggestions as variables? I can't seem to find anything that explains this.
You can use SimpleXmlIterator. That's really easy to use and you will be able to perform a foreach on the object you will get.
Library source
For example with file_get_contents or replace with curl if you prefer:
$feed = new SimpleXmlIterator(file_get_contents('http://www.dictionaryapi.com/api/v1/references/collegiate/xml/gobabola?key=135a6187-af83-4e85-85c1-1a28db11d5da'));
foreach ($feed->suggestion as $suggestion) {
echo $value;
}

Extracting XML data to php [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I'm trying to extract data from XML file (http://freegeoip.net/xml/google.com). You can see the content of the file looks something like:
<Response>
<Ip>74.125.235.3</Ip>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
<RegionCode>CA</RegionCode>
<RegionName>California</RegionName>
<City>Mountain View</City>
<ZipCode>94043</ZipCode>
<Latitude>37.4192</Latitude>
<Longitude>-122.0574</Longitude>
<MetroCode>807</MetroCode>
<AreaCode>650</AreaCode>
</Response>
I want to take the information stored in the <latitude> and <longitude> tags, and store them in separate variables. The problem is, I've little idea how to do this, and was wondering if anyone could show me how to parse XML files with php?
$string_data = "<your xml response>";
$xml = simplexml_load_string($string_data);
$latitude = (string) $xml->Latitude;
$longitude = (string) $xml->Longitude;
echo $latitude.' '.$longitude;
It's easy, use PHP's SimpleXML Library:
$xml = simplexml_load_file("http://freegeoip.net/xml/google.com");
echo $xml->Ip; // 173.194.38.174
echo $xml->CountryCode; // US
echo $xml->ZipCode; // 94043
// etc...
The PHP manual has a whole section on PHP parsing:
http://php.net/manual/en/book.simplexml.php
http://php.net/manual/en/book.xml.php
For simplicity, you could also use xml_parse_into_struct()
Here's a pretty good example, using SimpleXML:
http://blog.teamtreehouse.com/how-to-parse-xml-with-php5

Use SimpleXML to grab nested code [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
php simplexml issue in reading an attribute that has a ‘column - : ’ in its name
I'm in the process of learning PHP, so I'm making a weather app to display an image based on the weather. I'm using the Yahoo Weather API.
My problem is that I'm looking for a way to grab the code of the weather, located next to yweather:condition . I found simpleXML but can't figure out how to grab something that's located in something like the way Yahoo does its xml in the form of
<yweather:condition text="Fair" code="34" temp="37"
So my question is how can I grab the code="34" portion of the Yahoo weather and display it as a variable like $weathercode = 34;?
Thanks for any and all help, and I'm here to give more details if you need them!
Also, I'm not sure if the title of this post is correct, so sorry if it's not!
You can use the SimpleXMLElement::attributes call in a similar way to below.
$string = {Yahoo Weather Feed}
$xml = simplexml_load_string($string);
$weathercode = $xml->channel->item->children('yweather', true)->condition[0]->attributes()->code;
You may need to tinker to get it right, but this is just a guide.
Reference: http://php.net/manual/en/simplexmlelement.attributes.php
You could try the following:
$xml = simplexml_load_file('http://weather.yahooapis.com/forecastrss?w=12793465&u=f');
var_dump($xml);
This will load the URL and dump the SimpleXML object.
More on simplexml_load_file here

Tags still remaining after using simplexml_load_string [duplicate]

This question already has answers here:
Getting actual value from PHP SimpleXML node [duplicate]
(4 answers)
Closed 8 years ago.
I am using simplexml_load_string for XML packets. In my scenario, the XML string I want to convert is known as k.
My problem, however, is that when I use k, tags still remain that weren't parsed (<k>, <\k>).
For example, I use
$x->k, and I get back <k>DATA I WANT HERE<\EK>.
How do I get rid of these?
What the code does: It connects to a game and logs in.
Use InnerNode to get the value without the tags:
$x->k->InnerNode
You can also do a typecast:
(string)$x->k
I tried this and seem to be getting the string.
<?php
$str = "<msg t='sys'><body action='rndK' r='-1'><k>qH~e9Gmt</k></body></msg>";
$xml = simplexml_load_string( $str );
echo $xml->body->k; // gives 'qH~e9Gmt'
?>

How to parse JSONP with PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Extract JSONP Resultset in PHP
I get the response in the following format. Im facing trouble on how to get inside "Plugin" variable and access other variables inside it.I used json_decode(), but i cant access the variables.
Plugin
(
{
"plugin_a":"abc",
"plugin_b":"abc",
"plugin_c":"abc"
}
)
I tried
$a = json_decode($json,true);
echo $a['plugin_a'];
I dont get any output.
echo var_dump($json); gives me
string 'Plugin({
"plugin_a":"abc",
"plugin_b":"abc",
"plugin_ce":"abc" })'
try substr();
http://sandbox.phpcode.eu/g/40c20.php
<?php
$json = substr('Plugin
(
{
"plugin_a":"abc",
"plugin_b":"abc",
"plugin_c":"abc"
}
)', 9, -1);
print_r(json_decode($json));
Perhaps this will work for you:
$data=array('plugin_a'=>'abc','plugin_b'=>'bcd','plugin_c'=>'cde');
$json='{"Plugin":'.json_encode($data).'}';
$a=json_decode($json,true);
echo $a['Plugin']['plugin_a'];
It appears as though the actual json array may not have integrity. If this solution doesn't fit, can you post the code that actually builds the json array?

Categories