google geolocator:直接 URL 有效,使用 simplexml_load_file 时状态代码 620

发布于 2025-01-07 06:46:39 字数 3515 浏览 0 评论 0原文

我对这个有点困惑。

我正在尝试查找 lats & lngs 用于商店定位器,并一直在使用 Google 的 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);      

    } 

}     

?>

I'm a little confused about this one.

I'm trying to lookup lats & lngs for a store locator and have been working with Google's Geolocator and their tutorial at http://code.google.com/apis/maps/articles/phpsqlgeocode.html, but can't seem to return anything other than the dreaded 620 Too many queries error.

What's really strange to me is that if I copy my $request_url into a browser I get all the XML as expected, but using PHP's simplexml_load_file($request_url) always returns error code 620, no matter how long of a delay, no matter how many different API keys I use.

Does anyone know what's up with this? Am I making some horrendous mistake?

My code is below, please excuse extensive comments and echoes:

<?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);      

    } 

}     

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放低过去 2025-01-14 06:46:39

您很可能发送太多请求且速度太快。

请参阅 Google 地图中的 G_GEO_TOO_MANY_QUERIES 常量文档。

给定的密钥已超过 24 小时内的请求限制
或者在太短的时间内提交了太多请求。
如果您并行或紧密循环地发送多个请求,请在代码中使用计时器或暂停,以确保您不会发送
请求太快。

You are sending too many requests too fast most likely.

See G_GEO_TOO_MANY_QUERIES constant from the Google Maps documentation.

The given key has gone over the requests limit in the 24 hour period
or has submitted too many requests in too short a period of time.
If you're sending multiple requests in parallel or in a tight loop, use a timer or pause in your code to make sure you don't send the
requests too quickly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文