我正在使用此代码查找位置的纬度
$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]:无法打开流:HTTP请求失败!第139行的C:\ wamp \ www \ new_yellowpages \ modules \ mod_yellowpages_item_map \ helper.php中的HTTP / 1.0 400错误请求
如果有人知道这个问题请帮助我。
答案 0 :(得分:23)
使用curl代替file_get_contents
:
$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=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;
答案 1 :(得分: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);
答案 2 :(得分:1)
使用urlenocde函数对您的地址进行编码(如Google Map Standard),然后将Http更改为https。希望这对您有用
答案 3 :(得分:0)
尝试将HTTP更改为HTTPS并将''替换为'+'
https://maps.googleapis.com/maps/api/geocode/json?address=United+States+Neversink&sensor=false
答案 4 :(得分:-1)
使用curl_init()代替file_get_contents():
$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=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;