2012-01-05 43 views
0

我試圖使用PHP和捲曲獲得來自谷歌Places API的數據,但捲曲是給我的錯誤「無法連接到主機」。如果我將Google地方信息請求網址粘貼到瀏覽器中,則可以正常使用。 file_get_contents也沒有工作。這裏是我的代碼:無法連接到主機錯誤與谷歌Places API的和PHP /捲曲

$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密鑰。

+0

我不是一個PHP的傢伙,自動捲曲URL編碼請求? – 2012-01-05 16:09:19

+0

我試着URL編碼請求,沒有任何改變 – user1132322 2012-01-07 22:11:01

回答

0

我真的很喜歡使用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密鑰:)希望這將是有益的。

+0

file_get_contents只是給了我一個空白頁面,並使用fsockopen,連接超時 – user1132322 2012-01-07 22:10:38

相關問題