php google geocode rest api 请求没有响应

getting no response from php google geocode rest api request

我没有收到此 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);

你从来没有费心告诉 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));
}