简体   繁体   English

无法使用Google Places API和PHP / cURL连接到主机错误

[英]Couldn't Connect To Host Error with Google Places API and PHP/cURL

I'm trying to use PHP and cURL to get data from the Google Places API, but cURL is giving me the error "Couldn't connect to host". 我正在尝试使用PHP和cURL从Google Places API获取数据,但是cURL给我错误“无法连接到主机”。 If I paste the Google Places request URL in the browser, it works fine. 如果我将Google地方信息请求网址粘贴到浏览器中,则可以正常运行。 file_get_contents didn't work either. file_get_contents也不起作用。 Here is my code: 这是我的代码:

$ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    //curl_setopt($ch, CURLOPT_POST, 1);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldString);
    $result = curl_exec ($ch);
    if (curl_errno($ch)) {
            return curl_error($ch);
    } else {
    curl_close($ch);
    }
    return $result;

The URL I'm requesting is https://maps.googleapis.com/maps/api/place/search/json?key=xxx&location=27.916766641249062,-82.08984375&radius=50000&sensor=true&types=bar%7Cnight_club . 我请求的网址是https://maps.googleapis.com/maps/api/place/search/json?key=xxx&location=27.916766641249062,-82.08984375&radius=50000&sensor=true&types=bar%7Cnight_club I removed my API key for security purposes. 为了安全起见,我删除了API密钥。

I really like using file_get_contents() when possible, it is so simple and elegant. 我真的很喜欢在可能的情况下使用file_get_contents() ,它是如此简单而优雅。

Usually I do 通常我会

<?php
$json = (array) json_decode(file_get_contents("https://maps.googleapis.com/maps/api/place/search/json?key=xxx&location=27.916766641249062,-82.08984375&radius=50000&sensor=true&types=bar%7Cnight_club."));

var_dump($json);
?>

then you should see an array like this : 那么您应该看到这样的数组:

array(3) {
  ["html_attributions"]=>
  array(0) {
  }
  ["results"]=>
  array(0) {
  }
  ["status"]=>
  string(14) "REQUEST_DENIED"
}

Obviously, I don't have a correct API key :) hope it will be useful. 显然,我没有正确的API密钥:)希望它会有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM