問題描述
在我的一個 php 應用程序中,我必須從地址中找出該地點的緯度和經度.
In one of my php applications I have to find out the latitude and longitude of the place from address.
我試過這個代碼:
$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=$region");
$json = json_decode($json);
$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
但它顯示以下錯誤:
警告:file_get_contents(http://maps.google.com/maps/api/geocode/json?address=technopark, Trivandrun, kerala,India&sensor=false®ion=IND) [function.file-get-內容]:無法打開流:HTTP 請求失敗!第 4 行 D:Projectslon.php 中的 HTTP/1.0 400 Bad Request
Warning: file_get_contents(http://maps.google.com/maps/api/geocode/json?address=technopark, Trivandrun, kerala,India&sensor=false®ion=IND) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in D:Projectslon.php on line 4
請幫我解決這個問題.
推薦答案
使用 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;
這篇關于使用php、API自動獲取經緯度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!