簡體   English   中英

無法使用Google Places API和PHP / cURL連接到主機錯誤

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

我正在嘗試使用PHP和cURL從Google Places API獲取數據,但是cURL給我錯誤“無法連接到主機”。 如果我將Google地方信息請求網址粘貼到瀏覽器中,則可以正常運行。 file_get_contents也不起作用。 這是我的代碼:

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

我請求的網址是https://maps.googleapis.com/maps/api/place/search/json?key=xxx&location=27.916766641249062,-82.08984375&radius=50000&sensor=true&types=bar%7Cnight_club 為了安全起見,我刪除了API密鑰。

我真的很喜歡在可能的情況下使用file_get_contents() ,它是如此簡單而優雅。

通常我會

<?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);
?>

那么您應該看到這樣的數組:

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

顯然,我沒有正確的API密鑰:)希望它會有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM