Warning while using file_get_contents for google api

后端 未结 5 899
刺人心
刺人心 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 06:50

    Use urlenocde function to encode your address like google map standard and change Http to https. Hope this will work for you

    0 讨论(0)
  • 2020-12-17 06:56

    Try change HTTP to HTTPS and replace ' ' to '+'

    https://maps.googleapis.com/maps/api/geocode/json?address=United+States+Neversink&sensor=false
    
    0 讨论(0)
  • 2020-12-17 07:00

    Use urlencode to encode the address part. (The problem is the space in the address.)

    $address = urlencode("United States Neversink");
    $api='http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false';
    
    $result = file_get_contents($api);
    
    0 讨论(0)
  • 2020-12-17 07:03

    Use curl instead of 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;
    
    0 讨论(0)
  • 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&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;
    
    0 讨论(0)
提交回复
热议问题