我目前正在使用谷歌地圖API的地理編碼PHP功能。奇怪的是,我想,file_get_contents()返回bool(false),而我使用的url是正確編碼的。php file_get_contents()返回false與一個有效的網址
在我的瀏覽器中,當我測試代碼時,頁面需要很長時間才能加載,並且地理編碼不起作用(當然,考慮到API不給我想要的東西)。
此外,我試圖使用捲曲,迄今沒有成功。
如果有人能幫助我,那會很棒!
非常感謝。
代碼:
function test_geocoding2(){
$addr = "14 Boulevard Vauban, 26000 Valence";
if(!gc_geocode($addr)){
echo "false <br/>";
}
}
function gc_geocode($address){
$address = urlencode($address);
$url = "http://maps.google.com/maps/api/geocode/json?address={$address}";
$resp_json = file_get_contents($url);
$resp = json_decode($resp_json, true);
if($resp['status']=='OK'){
$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];
if($lati && $longi){
echo "(" . $lati . ", " . $longi . ")";
}else{
echo "data not complete <br/>";
return false;
}
}else{
echo "status not ok <br/>";
return false;
}
}
UPDATE:這個問題確實是事實,我是在一個代理。我測試了另一個網絡,它正常工作。 但是,您對我返回的內容以及我如何測試成功的回答也非常好,並且可以幫助我改進代碼。
非常感謝!
最有可能使用的是互聯網的一些代理。 http://stackoverflow.com/questions/5211887/how-to-use-curl-via-a-proxy – Ali
你永遠不會在'gc_geocode()'函數中返回'false'以外的任何東西......你'echo'其他的東西,但從來沒有返回'true' – Ben
嘗試'if(gc_geocode($ addr)=== false)' –