Ich habe eine MySQL-Tabelle in folgendem Format mit rund 500 Zeilenphp langsame Reaktionszeit mit Google Maps API
ID¦name¦stuff¦location
Mein PHP-Skript erhält einen Benutzer Lage, wählt alle Zeilen aus der Datenbank und dann bekommt die Entfernung von jedem Zeilen Standort zum Benutzer Standort mit der Google Maps API und legt alles in einem JSON-Array
Das Problem ist sogar die Verarbeitung von 500 Zeilen dauert 300 Sekunden im Durchschnitt, ich wollte es auf etwa 3- 4 Sekunden irgendwie, es scheint die Google Maps API zu sein, die das gesamte Skript bottlenecking.
Was ich frage ist, ob es eine bessere Möglichkeit gibt, mit der ganzen Sache umzugehen, damit ich das Skript in weniger als 10 Sekunden verarbeiten kann.
die PHP-Skript -
<?php
//used for benchmarking
$time_start = microtime(true);
require_once 'db.php';
//get the users location
$location = $_GET['location'];
//check the users location is valid
if (checkLocation($location) == "found") {
echo "null";
} else {
//select all of the rows
$SQLSelect = $odb -> query("SELECT * FROM `something`");
$array = array();
$count = 0;
while ($show = $SQLSelect -> fetch(PDO::FETCH_ASSOC)) {
$distance = getDistance($location, $show['location']);
$distance = $distance * 0.000621371192;
//narrow down the distance
if ($distance < 100) {
$array[$count] = array();
$array[$count]['name'] = $show['locationName'];
$array[$count]['spec'] = $show['spec'];
$array[$count]['distance'] = round($distance);
$count = $count + 1;
}
}
//sort the array based on distance
usort($array,function($a,$b) {return strnatcasecmp($a['distance'],$b['distance']);});
echo json_encode($array);
}
function getDistance($start, $end) {
$from = $start;
$to = $end;
$from = urlencode($from);
$to = urlencode($to);
$data = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&language=en-EN&sensor=false");
$data = json_decode($data);
$time = 0;
$distance = 0;
foreach($data->rows[0]->elements as $road) {
$time += $road->duration->value;
$distance += $road->distance->value;
}
return $distance;
}
function checkLocation($start) {
$from = $start;
$to = 'location1';
$from = urlencode($from);
$to = urlencode($to);
$data = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&language=en-EN&sensor=false");
if (strpos($data, '"status" : "NOT_FOUND"') !== false) {
return "found";
} else {
return "null";
}
}
echo '</br>Total execution time in seconds: ' . (microtime(true) - $time_start);
?>
werden Sie nie schaffen, 500 Abfragen an die Google Maps API in 10s ~ Sie könnten besser mit PHP die Entfernungen basierend auf einem Port der Havershine (?) Formel – RamRaider
Ich versuchte mit etwas ähnliches, aber es gefunden um ganz ungenau zu sein –