3

我正在使用此代码查找位置的纬度

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

$result = file_get_contents($api);

$data = json_decode($result);

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

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

4

5 回答 5

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;
于 2012-09-06T06:16:30.210 回答
14

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

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

$result = file_get_contents($api);
于 2012-09-06T06:17:37.347 回答
1

使用 urlenocde 函数对您的地址进行编码,如谷歌地图标准,并将 Http 更改为 https。希望这对你有用

于 2019-04-03T15:11:48.720 回答
0

尝试将 HTTP 更改为 HTTPS 并将 ' ' 替换为 '+'

https://maps.googleapis.com/maps/api/geocode/json?address=United+States+Neversink&sensor=false
于 2017-03-22T20:12:59.467 回答
-2

使用 curl_init() 而不是 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;
于 2018-09-05T09:09:58.133 回答