【问题标题】:Warning while using file_get_contents for google api将 file_get_contents 用于 google api 时发出警告
【发布时间】:2012-08-30 21:43:53
【问题描述】:

我正在使用此代码来查找位置的纬度

$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]: failed to打开流:HTTP 请求失败!第 139 行 C:\wamp\www\new_yellowpages\modules\mod_yellowpages_item_map\helper.php 中的 HTTP/1.0 400 错误请求

如果有人知道这个问题,请帮助我。

【问题讨论】:

    标签: php google-maps


    【解决方案1】:

    使用 curl 代替 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;
    

    【讨论】:

      【解决方案2】:

      使用urlencode 对地址部分进行编码。 (问题是地址中的空格。)

      $address = urlencode("United States Neversink");
      $api='http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false';
      
      $result = file_get_contents($api);
      

      【讨论】:

      • 两个答案的组合(Abid & xdazz)帮助了我!
      【解决方案3】:

      使用 urlenocde 函数像谷歌地图标准一样对您的地址进行编码,并将 Http 更改为 https。希望这对你有用

      【讨论】:

        【解决方案4】:

        尝试将 HTTP 更改为 HTTPS 并将 ' ' 替换为 '+'

        https://maps.googleapis.com/maps/api/geocode/json?address=United+States+Neversink&sensor=false
        

        【讨论】:

          【解决方案5】:

          使用 curl_init() 代替 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;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-01-24
            • 1970-01-01
            • 2012-10-24
            • 2017-10-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多