【问题标题】:getting no response from php google geocode rest api request没有从 php google geocode rest api 请求得到响应
【发布时间】:2015-12-11 19:07:30
【问题描述】:

我没有从这个 php 代码中得到任何响应,也没有错误。请问有谁知道我做错了什么?看起来很简单:

php:

$details_url = "https://maps.googleapis.com/maps/api/geocode/json?address=436+Grant+Street+Pittsburgh&sensor=false&key=mykey";
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_HEADER, 0);
   $response = curl_exec($ch);
   print_r($response);

【问题讨论】:

    标签: php curl google-api geocode


    【解决方案1】:

    你从来没有费心告诉 curl 你的 url。你应该有

    $ch = curl_init($details_url);
                    ^^^^^^^^^^^^
    

    curl_setopt($ch, CURLOPT_URL, $details_url);
    

    请注意 print_r 不是一个好的调试工具。你可能从 curl_exec 得到一个布尔值 false,print_r 根本不会显示:

    php > $x = false;
    php > print_r($x);
    php > var_dump($x);
    bool(false)
    

    更好的选择是

    $response = curl_exec($ch);
    if ($response === false) {
        die(curl_error($ch));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-09
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-22
      • 2018-02-05
      相关资源
      最近更新 更多