【问题标题】:php file_get_contents() returning false with a valid urlphp file_get_contents() 使用有效的 url 返回 false
【发布时间】:2016-10-24 06:21:55
【问题描述】:

我目前正在使用谷歌地图 API 进行地理编码 php 函数。奇怪的是,file_get_contents() 返回 bool(false) 而我使用的 url 编码正确,我认为。

在我的浏览器中,当我测试代码时,页面需要很长时间才能加载,并且地理编码不起作用(当然,鉴于 API 没有给我想要的东西)。

我也试过用curl,目前没有成功。

如果有人可以帮助我,那就太好了!

非常感谢。

代码:

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;
    }
}

更新:问题确实是我在代理后面。我用另一个网络测试过,它工作正常。 但是,您对我返回的内容以及我如何测试成功的回答也非常好,并且将帮助我改进代码。

非常感谢!

【问题讨论】:

标签: php api google-maps geocoding


【解决方案1】:

看看if声明:

if(!gc_geocode($addr)){
    echo "false <br/>";
}

这意味着如果gc_geocode($addr) 返回falsenull,则此语句将回显“false”。

但是,您实际上从未从函数中返回任何内容,所以如果成功,它将返回 null

$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($lati && $longi){
        echo "(" . $lati . ", " . $longi . ")"; //ECHO isn't RETURN
        /* You should return something here, e.g. return true */
    } else {
        echo "data not complete <br/>";
        return false;
    }
} else {
    echo "status not ok <br/>";
    return false;
}

或者,您可以将if 语句更改为仅在函数返回false 时触发:

if(gc_geocode($addr)===false){
    //...

【讨论】:

    【解决方案2】:

    当你没有返回任何东西时,函数返回null
    只需使用它:

    if(!is_null(gc_geocode($addr))) {
        echo "false <br/>";
    }
    

    或者:

    if(gc_geocode($addr) === false) {
        echo "false <br/>";
    }
    

    【讨论】:

      【解决方案3】:

      上面的函数 gc_geocode() 在我的系统上正常工作,没有任何额外的负载。你已经调用了 gc_geocode () 它返回你 lat, long 现在你已经检查过了

      if(!gc_geocode($addr)){
          echo "false <br/>";
      } 
      

      使用

        if($responce=gc_geocode($addr)){
            echo $responce;       
          }
        else{
           echo "false <br/>";
          } 
      

      【讨论】:

        【解决方案4】:

        问题在于我使用的是代理。代码是正确的。

        要检查您和 Internet 之间是否存在代理,您必须了解网络的基础架构。如果您在学校或公司网络工作,很可能会使用代理来保护本地网络。 如果您不知道答案,请询问您的网络管理员

        如果您的网络中没有声明代理,仍有可能存在透明代理。但是,正如该问题的公认答案所述:https://superuser.com/questions/505772/how-can-i-find-out-if-there-is-a-proxy-between-myself-and-the-internet-if-there

        如果它是透明代理,您将无法在客户端 PC 上检测到它。

        一些网站还提供了一些代理检测器,但我不知道那里提供的信息有多相关。这里有两个例子:

        http://amibehindaproxy.com/

        http://www.proxyserverprivacy.com/free-proxy-detector.shtml

        【讨论】:

          猜你喜欢
          • 2013-01-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-24
          • 1970-01-01
          • 2019-01-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多