I hve been working locally with a geocode script to add lat and long pinpoints on a google map. Locally it has been successful and rewarding, however now I am working on a live server I have found that URl file access is disable and I can't use this. I have been reading around and a lot of sites suggest Curl to get around this. Now the xml was bad enough but now I have been trying to use Curl to get my desired effect but with little results. I have been stuck at a 400 error for the last 5 hours and really don't know what else to do with this.
Here is my code;
define("MAPS_HOST", "maps.google.com");
define("KEY", "mykey");
// Set the active MySQL database
$db_selected = mysql_select_db($database_stuff, $database_stuff);
if (!$db_selected) {
die("Can\'t use db : " . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM stockists WHERE 1";
$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;
$ch = curl_init("http://maps.google.com/maps/geo?output=xml&key=mykey");
$fp = fopen("example_htmlpage.html", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
// Iterate through the rows, geocoding each address
while ($row = @mysql_fetch_assoc($result)) {
$geocode_pending = true;
while ($geocode_pending) {
$address = $row["stockist_address"];
$stockist_id = $row["stockist_id"];
$request_url = $ch . "&q=" . urlencode($address);
$xml = simplexml_load_file("example_htmlpage.html") or die("url not loading");
$status = $xml->Response->Status->code;
if (strcmp($status, "200") == 0) {
// Successful geocode
$geocode_pending = false;
$coordinates = $xml->Response->Placemark->Point->coordinates;
$coordinatesSplit = split(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $coordinatesSplit[1];
$lng = $coordinatesSplit[0];
$query2 = sprintf("UPDATE stockists " .
" SET lat = '%s', lng = '%s' " .
" WHERE stockist_id = '%s' LIMIT 1;",
mysql_real_escape_string($lat),
mysql_real_escape_string($lng),
mysql_real_escape_string($stockist_id));
$update_result = mysql_query($query2);
if (!$update_result) {
die("Invalid query: " . mysql_error());
}
} else if (strcmp($status, "620") == 0) {
// sent geocodes too fast
$delay += 100000;
} else {
// failure to geocode
$geocode_pending = false;
echo "Address " . $address . " failed to geocoded. ";
echo "Received status " . $status . "
\n";
}
usleep($delay);
}
}
print "WORKING";
exit;
It does not seem to like the url but this looks ok to me. But then this is my first time using this so I would really appreaciate some experienced opinions please.
Thanks a bunch