我對這個有點困惑。谷歌geolocator:直接URL的作品,使用simplexml_load_file時的狀態碼620
我試圖查找lats & lngs的商店定位器,並一直與谷歌的Geolocator和他們的教程在http://code.google.com/apis/maps/articles/phpsqlgeocode.html,但似乎無法返回除了可怕的620太多的查詢錯誤之外的任何東西。
對我來說真的很奇怪的是,如果我將我的$ request_url複製到瀏覽器中,我會按預期得到所有的XML,但使用PHP的simplexml_load_file($ request_url)總是返回錯誤代碼620,無論延遲多長時間,不管我使用多少個不同的API密鑰。
有人知道這是怎麼回事嗎?我犯了一些可怕的錯誤嗎?
我的代碼如下,請原諒廣泛徵求意見和回聲:
<?php
$host = 'xxx';
$db = 'xxx';
$u = 'xxx';
$p = 'xxx';
define("MAPS_HOST", "maps.google.com");
define("KEY", "xxx");
// Opens a connection to a MySQL server
$connection = mysql_connect($host, $u, $p);
if (!$connection) {
die("Not connected : " . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($db, $connection);
if (!$db_selected) {
die("Can\'t use db : " . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM BH_locations";
$result = mysql_query($query);
if (!$result) {
die("Invalid query: " . mysql_error());
}
// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;
// Iterate through the rows, geocoding each address
while ($row = @mysql_fetch_assoc($result)) {
echo '<br /><br />row#' . $row['id'] . '<br />';
$geocode_pending = true;
while ($geocode_pending) {
echo 'started while loop at ' . date('G:i:s:u') . '<br />';
$address = $row["address"] . ', ' . $row['city'] . ', ' . $row['state'] . ' ' . $row['zip'];
echo $address . '<br />';
$id = $row["id"];
$request_url = $base_url . "&q=" . urlencode($address);
echo 'request url: ' . $request_url . '<br />';
$xml = simplexml_load_file($request_url) or die("url not loading");
echo '<pre>';
var_dump($xml);
echo '</pre>';
$status = $xml->Response->Status->code;
if (strcmp($status, "200") == 0) {
// Successful geocode
echo '<br /><strong>WOOHOO IT WORKED!!!</strong><br />';
$geocode_pending = false;
/*
$coordinates = $xml->Response->Placemark->Point->coordinates;
$coordinatesSplit = split(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $coordinatesSplit[1];
$lng = $coordinatesSplit[0];
$query = sprintf("UPDATE BH_locations " .
" SET latitude = '%s', longitude = '%s' " .
" WHERE id = '%s' LIMIT 1;",
mysql_real_escape_string($lat),
mysql_real_escape_string($lng),
mysql_real_escape_string($id));
$update_result = mysql_query($query);
if (!$update_result) {
die("Invalid query: " . mysql_error());
}
*/
} else if (strcmp($status, "620") == 0) {
// sent geocodes too fast
$delay += 100000;
echo 'error 620<br /><br />';
} else {
// failure to geocode
$geocode_pending = false;
echo "Address " . $address . " failed to geocoded. ";
echo "Received status " . $status . "\n";
}
usleep($delay);
}
}
?>
你可以提供'$ request_url'嗎?你只對一個地址進行地理編碼或僅測試一個地址?你只看到一個錯誤(620)?或者你看到你的一些成功的消息(*嗚呼它工作*)? – Josh 2012-02-17 21:18:33
哎呀,抱歉沒有指定數據量 - 大約有350個地址。它永遠不會超過第一個,只是一遍又一遍地重複620錯誤,直到我的服務器超時。 request_url爲http://maps.google.com/maps/geo?output=xml&key=[MY API KEY]&q = [ADDRESS] – jabram 2012-02-17 22:07:16
您是否有共享主機?目前似乎沒有在文檔中提到這一點,但互聯網上提到每個IP限制以及每個IP超過請求限制的IP禁止。在共享主機上,這很有可能,並且可以解釋爲什麼在您的瀏覽器中將URL粘貼到地址欄時不會失敗(請考慮:您的IP與共享主機IP)。 – bububaba 2012-02-17 22:09:06