使用google api的file_get_contents时出现警告

Man*_*hta 2 php google-maps

我正在使用此代码查找某个位置的lat lon

$api='http://maps.googleapis.com/maps/api/geocode/json?address=United States Neversink&sensor=false';

$result = file_get_contents($api);

$data = json_decode($result);
Run Code Online (Sandbox Code Playgroud)

但它会发出警告警告:file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=United States Neversink&sensor = false)[function.file-get-contents]:无法打开流: HTTP请求失败!第139行的C:\ wamp\www \new_yellowpages\modules\mod_yellowpages_item_map\helper.php中的HTTP/1.0 400错误请求

如果有人知道这个问题请帮助我.

Abi*_*ain 23

使用curl而不是file_get_contents:

$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=India";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
echo $lat = $response_a->results[0]->geometry->location->lat;
echo "<br />";
echo $long = $response_a->results[0]->geometry->location->lng;
Run Code Online (Sandbox Code Playgroud)


xda*_*azz 12

使用urlencode对地址部分进行编码.(问题是地址中的空格.)

$address = urlencode("United States Neversink");
$api='http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false';

$result = file_get_contents($api);
Run Code Online (Sandbox Code Playgroud)