2015-06-09 48 views
0

我没有得到任何来自谷歌的地方文本搜索API响应没有从谷歌的地方文本搜索API响应

这里是我的PHP代码:

$string=$row["PropertyTitle"].'+'.$row["LocalityName"].'+'.$row["CityName"]; 
$details_url="https://maps.googleapis.com/maps/api/place/textsearch/json?query=".$city."&key=someapikey"; 
curl_setopt($ch, CURLOPT_URL, $details_url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$geoloc = json_decode(curl_exec($ch), true); 
print_r($geoloc);

它甚至不给下面的结果

{ 
    "html_attributions" : [], 
    "results" : [], 
    "status" : "INVALID_REQUEST" 
}

我在做什么错误以及如何解决这个问题?

+0

哦,太棒了,您发布了您的API密钥。顺便说一句,你是否正确地编码你的'$城市'? – Raptor

+1

您正在查询参数中传递'$ city',但好像您尚未定义。 – WisdmLabs

+0

@WisdmLabs很好的接收! – Raptor

回答

1

你缺少$ch = curl_init();

我猜你$string应该命名为$city。另外,在$city上使用urlencode。请参阅Why should I use urlencode?

<?php 
$city = urlencode($row["PropertyTitle"].'+'.$row["LocalityName"].'+'.$row["CityName"]); 
$details_url="https://maps.googleapis.com/maps/api/place/textsearch/json?query=".$city."&key=API_KEY"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $details_url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$geoloc = json_decode(curl_exec($ch), true); 
print_r($geoloc);