I agree with the original poster of the question. To be very specific:
http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=301+E.+Linwood+Avenue++Turlock%2C+CA
This requires the sensor=false variable to be passed, or the query will return BAD results from Google. If I pass this STRING through file_get_contents, it (PHP file_get_contents) replaces the "&" with "&"
so Google doesn't like me:
Array
(
[type] => 2
[message] => file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=301 E. Linwood Avenue Turlock, CA&sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
)
So this is the solution I came up with, using http_build_query
$myURL = 'http://maps.googleapis.com/maps/api/geocode/json?';
$options = array("address"=>$myAddress,"sensor"=>"false");
$myURL .= http_build_query($options,'','&');
$myData = file_get_contents($myURL) or die(print_r(error_get_last()));
I also include the code (thanks Marco K.) I found on the PHP website to use a custom function for PHP < 5:
if (!function_exists('http_build_query')) {
function http_build_query($data, $prefix='', $sep='', $key='') {
$ret = array();
foreach ((array)$data as $k => $v) {
if (is_int($k) && $prefix != null) {
$k = urlencode($prefix . $k);
}
if ((!empty($key)) || ($key === 0)) $k = $key.'['.urlencode($k).']';
if (is_array($v) || is_object($v)) {
array_push($ret, http_build_query($v, '', $sep, $k));
} else {
array_push($ret, $k.'='.urlencode($v));
}
}
if (empty($sep)) $sep = ini_get('arg_separator.output');
return implode($sep, $ret);
}// http_build_query
}//if
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…