PHP 谷歌地图地理编码 XML 加载问题
这真的很奇怪,让我感到困惑。我有一个 PHP 脚本,它从地址输入表单收集数据,将其格式化为 googlemaps 地理编码服务,然后加载该服务返回的 xml 并从中提取纬度/经度。奇怪的是,某些地址返回“url not loading”死消息,但是当我复制脚本发送的回显的确切字符串并将其粘贴到我的浏览器中时,我实际上得到了 xml 文件。有人知道这里会发生什么吗?
这是回显的字符串。也许有人可以告诉我 PHP 不喜欢这个 URL 的什么?
http://maps.google.com/maps/geo?output=xml&key=ABQIAAAAZe-W-lkccZSM2qC_Ci4J nBS97eShVNVCdPw2dK9jd_Ls-9Sg7hSJTMXMmezqrmN9bDDbO_CSRwChtw&q=285+东+格兰德+大道,+福克斯 Lake,+IL,+60020&sensor=true_or_false
这是脚本:
//This section sends HTTP geocode to google and recieves the XML
$googlekey = "ABQIAAAAZe-W-lkccZSM2qC_Ci4JnBS97eShVNVCdPw2dK9jd_Ls-9Sg7hSJTMXMmezqrmN9bDDbO_CSRwChtw";
$mapAddress = urlencode($barAddress).",+".$barCity.",+".$barState.",+".$barZip;
echo $mapAddress."<br />";
$googleURL = "http://maps.google.com/maps/geo?output=xml&key=".$googlekey."&q=".$mapAddress."&sensor=true_or_false";
echo $googleURL."<br />";
$googlexml = simplexml_load_file($googleURL) or die("url not loading<br />");
$status = $googlexml - > Response - > Status - > code;
if (strcmp($status, "200") == 0) {
echo "barID:".$barID;
// Successful geocode
$coordinates = $googlexml - > Response - > Placemark - > Point - > coordinates;
$coordinatesSplit = split(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $coordinatesSplit[1];
$lng = $coordinatesSplit[0];
echo "lat:".$lat." lon:".$lng;
$xmlUpdate = "/".$barID."/".$barID.".xml";
//This is the final query that updates
$updateQuery = sprintf("UPDATE userBars "." SET lat = '%s', lng = '%s', barXML = '%s' "." WHERE barID = '%s';", mysql_real_escape_string($lat), mysql_real_escape_string($lng), mysql_real_escape_string($xmlUpdate), mysql_real_escape_string($barID));
$updateResult = mysql_query($updateQuery);
if ($updateResult) {
echo "Successfully Geocoded!<br />";
} else {
echo "Geocode Not Successful!";
}
} else {
echo "Geocode Failed.";
}
}
} else {
echo "Error retrieving barID. Files and entries were not created successfully.<br />";
}
This is really weird and has me baffled. I have a PHP script that collects data from an address input form, formats it for the googlemaps geocoding service, and then loads the xml this service returns and extracts the lat/lng out of it. What's weird is that certain addresses return the "url not loading" die message, but when I copy the exact string that is echod out that the script is sending and paste it into my browser, i do in fact get the xml file. Anyone have any idea what might be going on here?
Here is the string that is echo'd out. Maybe someone can tell me what PHP doesn't like about this URL?
http://maps.google.com/maps/geo?output=xml&key=ABQIAAAAZe-W-lkccZSM2qC_Ci4JnBS97eShVNVCdPw2dK9jd_Ls-9Sg7hSJTMXMmezqrmN9bDDbO_CSRwChtw&q=285+East+Grand+Avenue,+Fox Lake,+IL,+60020&sensor=true_or_false
Here is the script:
//This section sends HTTP geocode to google and recieves the XML
$googlekey = "ABQIAAAAZe-W-lkccZSM2qC_Ci4JnBS97eShVNVCdPw2dK9jd_Ls-9Sg7hSJTMXMmezqrmN9bDDbO_CSRwChtw";
$mapAddress = urlencode($barAddress).",+".$barCity.",+".$barState.",+".$barZip;
echo $mapAddress."<br />";
$googleURL = "http://maps.google.com/maps/geo?output=xml&key=".$googlekey."&q=".$mapAddress."&sensor=true_or_false";
echo $googleURL."<br />";
$googlexml = simplexml_load_file($googleURL) or die("url not loading<br />");
$status = $googlexml - > Response - > Status - > code;
if (strcmp($status, "200") == 0) {
echo "barID:".$barID;
// Successful geocode
$coordinates = $googlexml - > Response - > Placemark - > Point - > coordinates;
$coordinatesSplit = split(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $coordinatesSplit[1];
$lng = $coordinatesSplit[0];
echo "lat:".$lat." lon:".$lng;
$xmlUpdate = "/".$barID."/".$barID.".xml";
//This is the final query that updates
$updateQuery = sprintf("UPDATE userBars "." SET lat = '%s', lng = '%s', barXML = '%s' "." WHERE barID = '%s';", mysql_real_escape_string($lat), mysql_real_escape_string($lng), mysql_real_escape_string($xmlUpdate), mysql_real_escape_string($barID));
$updateResult = mysql_query($updateQuery);
if ($updateResult) {
echo "Successfully Geocoded!<br />";
} else {
echo "Geocode Not Successful!";
}
} else {
echo "Geocode Failed.";
}
}
} else {
echo "Error retrieving barID. Files and entries were not created successfully.<br />";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论