0

I'm currently trying to get the lat and lng parameters for an address through the use of the Google Geocode API in a PHP page.

I currently have the following code, but somehow it does not work through php while copying the generated address into Google Chrome does seem to work.

Can anybody see the error in the code below?

Thanks in advance!

Tom

====================================================

The returned error is:

( [error_message] => The 'sensor' parameter specified in the request must be set to either 'true' or 'false'. [results] => Array ( ) [status] => REQUEST_DENIED ) An error has occured: 1

Old code with obsolete parts:

$googleQuery = $_POST['txtAdres'] . ',+' . $_POST['txtPostcode'] . '+' . $_POST['txtStad'] . ',+' . $_POST['txtLand'];
$googleQuery = str_replace(' ', '+', $googleQuery);

// retrieve the latitude & longitude from the address
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($googleQuery) . '&sensor=false';
$url = htmlspecialchars($url);

echo $url;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = json_decode(curl_exec($ch), true);
if ($response['status'] != 'OK') {
  echo 'An error has occured: ' . print_r($response);
} else {
    $geometry = $response['results'][0]['geometry'];
    $longitude = $geometry['location']['lat'];
    $latitude = $geometry['location']['lng'];
}

====================================================

EDIT 1 - Added HTML code to each webpage to make it work

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

EDIT 1 - Fixed and working code:

// create address string
$googleQuery = $_POST['txtAdres'] . ', ' . $_POST['txtPostcode'] . ' ' . $_POST['txtStad'] . ', ' . $_POST['txtLand'];

// retrieve the latitude & longitude from the address
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($googleQuery) . '&sensor=false';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = json_decode(curl_exec($ch), true);
if ($response['status'] != 'OK') {
  echo 'An error has occured: ' . print_r($response);
} else {
    $geometry = $response['results'][0]['geometry'];
    $longitude = $geometry['location']['lat'];
    $latitude = $geometry['location']['lng'];
}

2 Answers 2

9

You have invalid $url. Don't do $url = htmlspecialchars($url); before calling CURL.

Change:

// ...
$url = htmlspecialchars($url);
echo $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// ...

to (or smth):

// ...
echo htmlspecialchars($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// ...

Edit 1:

And your address generating is invalid. Instead of:

$googleQuery = $_POST['txtAdres'] . ',+' . $_POST['txtPostcode'] . '+' . $_POST['txtStad'] . ',+' . $_POST['txtLand'];
$googleQuery = str_replace(' ', '+', $googleQuery);

do:

$googleQuery = $_POST['txtAdres'] . ', ' . $_POST['txtPostcode'] . ' ' . $_POST['txtStad'] . ', ' . $_POST['txtLand'];

Edit 2:

Here is valid example:

$googleQuery = 'Sint Elooisstraat 30, 8800 Roeselare, België';
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($googleQuery) . '&sensor=false';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = json_decode(curl_exec($ch), true);
print_r($response);
Sign up to request clarification or add additional context in comments.

9 Comments

Removing the htmlspecialchars made it even worse. Now the error message is { "results" : [], "status" : "INVALID_REQUEST" }
Can you show output for varible $url? p.s. I just tested this script and it works for me.
returned url with test data (without the htmlspecialchars): maps.googleapis.com/maps/api/geocode/… The error message is: Array ( [results] => Array ( ) [status] => INVALID_REQUEST ) An error has occured: 1 The url with htmlspecialchars: maps.googleapis.com/maps/api/geocode/…
Your address is invalid. Try this. Delete that str_replace() that replaces spaces to + and those + from concating post data into string; this is all done by urlecnode() function.
The output url is now: maps.googleapis.com/maps/api/geocode/… and the error message is still: Array ( [results] => Array ( ) [status] => INVALID_REQUEST ) An error has occured: 1
|
0

have had this issue within a CLI script.

i had to ensure that my data (which was imported from a CSV file) was UTF-8 encoded:

$address = utf8_encode($address);
$address = rawurlencode($address);  

also, a string i had to pass from within the script i had to convert to ASCII first before converting it to UTF-8 (or htmlentities, which also works):

mb_convert_encoding('Österreich', 'US-ASCII', 'UTF-8')

or

htmlentities('Österreich', ENT_COMPAT, 'UTF-8')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.