我想用symfony获取用户当前的lat和long我使用下面的代码,但它没有给我正确的结果,
$new_arr[] = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']));
if ($new_arr) {
$Latitude = $new_arr[0]['geoplugin_latitude'];
$Longitude = $new_arr[0]['geoplugin_longitude'];
}
有人可以帮我解决吗?
答案 0 :(得分:0)
考虑到你在这里不需要代理我用于项目的一段代码:
function getIP()
{
$ip = $_SERVER['REMOTE_ADDR'];
return ($ip == "::1") ? "" : $ip;
}
$location = simplexml_load_string(file_get_contents('http://freegeoip.net/xml/'.getIP(), false));
$longitude = $location->Longitude;
$latitude = $location->Latitude;
使用代理:
$opts = array('http' => array('proxy' => 'ASK YOUR SYSADMIN', "request_fulluri" => true));
$context = stream_context_create($opts);
function getIP()
{
$ip = $_SERVER['REMOTE_ADDR'];
return ($ip == "::1") ? "" : $ip;
}
$location = simplexml_load_string(file_get_contents('http://freegeoip.net/xml/'.getIP(), false, $context));
$longitude = $location->Longitude;
$latitude - $location->Latitude;
答案 1 :(得分:0)
您使用的API并未提供准确的输出,因此我建议您使用GeoIP
库。
以下是下载所有文件的链接:
请将所有这些文件放在一个文件夹中,并将GeoLiteCity.dat.gz
重命名为GeoLiteCity.dat
// I am assuming that folder name is geoip and then include all files
include ("geoip/geoipcity.inc");
include ("geoip/geoipregionvars.php");
$giCity = geoip_open("geoip/GeoLiteCity.dat", GEOIP_STANDARD);
$ip = $_SERVER['REMOTE_ADDR'];
$record = geoip_record_by_addr($giCity, $ip);
echo "Getting Country and City detail by IP Address <br /><br />";
echo "IP: " . $ip . "<br /><br />";
// this array will give you all the details like address, city, state etc.
print_r($record);
一切顺利!