This is my query:
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
$city= $details->city;
give result:
{
"ip": "210.56.147.31",
"hostname": "No Hostname",
"city": null,
"region": null,
"country": "IN",
"loc": "20.0000,77.0000",
"org": "AS45774 Chandra Net Pvt. Limited, India"
}
I can get loc by : $loc= $details->loc;
Which gives result: loc is : 20.0000,77.0000
for echo "loc is : $loc"
I want to get longitude and lattitude separately in $long
and $latt
. how could be done? how both values can be fetched separated?
$loc= $details->loc[0]; //this is not correct
Try splitting the value using explode()
. you can find details here
$loc = $details->loc;
$latLong = explode(",",$loc);
$longitude = $latLong[0];
$lattitude = $latLong[1];
Just explode the value on the ,
.
Example
$long = explode(',', $loc)[0];
$lat = explode(',', $loc)[1];
Note this will only work for later versions of PHP. If you're using an old version of PHP, you'll need to assign the array to a variable before referring to it's elements, like so:
$location = explode(',',$loc);
$lat = $location[0];
$long = $location[1];
You can use explode
to split the string, and list
to assign the two variables at the same time:
list($lat, $lon) = explode(",", $loc);