I have a mysql database of records that holds addresses. I want to iterate through these addresses and get the geocode lat and lng for each one and put them in the database. I am using php. But when I run this code, I get an error that says "The FastCGI process exceeded configured request timeout". Does anyone have a suggestion for how to make this program work better, or if not, whether another method will work better?
include_once ('constants_test.php');
// connect to database
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); //open db conn
if (mysqli_connect_errno()) {
printf("Connect failed: %s", mysqli_connect_error());
exit();
}
function lookup($string){
ini_set('max_execution_time', 300);
$string = str_replace (" ", "+", urlencode($string));
$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$string."&sensor=false";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
// If Status Code is ZERO_RESULTS, OVER_QUERY_LIMIT, REQUEST_DENIED or INVALID_REQUEST
if ($response['status'] != 'OK') {
return null;
}
//print_r($response);
$geometry = $response['results'][0]['geometry'];
$longitude = $geometry['location']['lat'];
$latitude = $geometry['location']['lng'];
$array = array(
'latitude' => $geometry['location']['lng'],
'longitude' => $geometry['location']['lat'],
);
return $array;
}
$q = "SELECT ShotLocation FROM shotsfired";
$result = $mysqli->query($q);
$count = 0;
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
if ($count < 3) {
$location = $row["ShotLocation"];
$location .= ", Pittsburgh, PA";
$array = lookup($location);
print_r($array);
print_r ("<br/><br/>");
}
$count = $count++;
}