Warning while using file_get_contents for google api

后端 未结 5 903
刺人心
刺人心 2020-12-17 06:49

I am using this code for finding the lat lon of a location

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


        
5条回答
  •  暖寄归人
    2020-12-17 07:12

    Use curl_init() instead of 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 "
    "; echo $long = $response_a->results[0]->geometry->location->lng;

提交回复
热议问题