2012-01-05 81 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

相关问题